简体   繁体   English

提高语言环境格式百分比

[英]boost locale format percentage

I have a problem with boost::locale::format outputting as a percentage. 我有一个boost :: locale :: format以百分比形式输出的问题。 According to the documentation ( http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/localized_text_formatting.html ): 根据文档( http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/localized_text_formatting.html ):

Numbers and number manipulators 数字和数字操纵器

Here are the manipulators for number formatting: 以下是数字格式的操纵器:

 as::number -- format number according to local specifications, it takes into account as::percent -- format number as "percent" format. For example: cout << as::percent << 0.25 <<endl; Would create an output that may look like this: 25% 

However, the following outputs "0.25": 但是,以下输出为“ 0.25”:

#include <string>
#include <iostream>
#include <boost/system/system_error.hpp>
#include <boost/locale.hpp>


int main(int argc, char** argv)
{
    std::cout << boost::locale::as::percent << 0.25 <<std::endl;
}

I have tried imbuing std::cout with an EN-US locale through std::locale() as well as a locale generated by boost::locale::generator to no avail. 我已经尝试通过std :: locale()以及EN-US语言环境将std :: cout注入到由boost :: locale :: generator生成的语言环境中 ,但无济于事。

It seems like I'm missing some obvious problem; 好像我缺少一些明显的问题; could anyone hint at it please? 有人可以暗示吗?

This works for me: 这对我有用:

#include <boost/locale.hpp>
#include <iostream>

int main()
{
  boost::locale::generator gen;
  std::locale loc = gen("en_US.UTF-8");
  std::cout.imbue(loc);
  std::cout << boost::locale::as::percent << 0.25 << std::endl;
  return 0;
}

Boost.Locale uses 4 backends (ICU, posix, winapi, std), but only ICU backend has percentage feature, that means the library you're using is not compiled with ICU library. Boost.Locale使用4个后端(ICU,posix,winapi,std),但是只有ICU后端具有百分比功能,这意味着您使用的库未与ICU库一起编译。

You can see which feature can be used with which backend in Supported Features table. 您可以在“ 支持的功能”表中查看可以与哪个后端一起使用的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM