我正在尝试做这个简单的任务。只是使用 C 或 C++ 格式化数字,但在 Windows CE 下编程。
在这种环境中,inbue 和 setlocale 方法都不起作用。
最后我这样做没有成功:
char szValue[10];
sprintf(szValue, "%'8d", iValue);
任何想法?
这是一种方法 - 创建一个自定义区域设置并为其注入适当的自定义方面:
#include <locale>
#include <iostream>
#include <memory>
struct separate_thousands : std::numpunct<char> {
char_type do_thousands_sep() const override { return ','; } // separate with commas
string_type do_grouping() const override { return "\3"; } // groups of 3 digit
};
int main()
{
int number = 123'456'789;
std::cout << "default locale: " << number << '\n';
auto thousands = std::make_unique<separate_thousands>();
std::cout.imbue(std::locale(std::cout.getloc(), thousands.release()));
std::cout << "locale with modified thousands: " << number << '\n';
}
预期输出:
default locale: 123456789
locale with modified thousands: 123,456,789
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)