要生成具有正确数字分隔符(“.”或“,”)的 csv 文件,因为我希望它们与计算机上安装的 Excel 版本兼容,所以我需要从 C++ 程序获取小数分隔符。
我的机器有法语版本的 Windows/Excel,因此小数点分隔符是“,”。
int main()
{
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
return 0;
}
outputs .
,这不是预期的
我尝试使用 WIN32 API:
int main()
{
TCHAR szSep[8];
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, szSep, 8);
std::cout << szSep;
}
outputs ,
,这是预期的。
有没有与此等效的GetLocaleInfo
STL 中的函数将在一个简单的内部工作main
?
感谢 user0042 链接的示例,使用 STL 执行此操作的适当方法是:
int main()
{
// replace the C++ global locale as well as the C locale with the user-preferred locale
std::locale::global(std::locale(""));
// use the new global locale for future wide character output
std::cout.imbue(std::locale());
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
}
outputs ,
,这是预期的。
或者,如果您不想更改全局:
int main()
{
std::cout.imbue(std::locale(""));
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)