b.b*_*old 9 c++ formatting localization numbers
虽然我自己写的东西很容易写,但我经常想知道在iomanip某个地方或某个地方是否有类似的东西.但是,我从未发现过有用的东西.理想情况下,它对区域设置很敏感(例如在德国,您将1,234,567.89写为1.234.567,89),因此非常优于手动构建逗号字符串.
根据此线程,您可以通过执行以下操作在输出流上设置区域设置:
#include <iostream>
#include <locale>
#include <string>
struct my_facet : public std::numpunct<char> {
explicit my_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\003"; }
};
int main() {
std::locale global;
std::locale withgroupings(global, new my_facet);
std::locale was = std::cout.imbue(withgroupings);
std::cout << 1000000 << std::endl;
std::cout.imbue(was);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我自己没试过,但它听起来确实是一种合理的方法.