[英]How to get system's decimal separator character using STL?
To generate csv files with the correct numerical separator ('.' or ','), because I want them to be compatible with Excel version installed on the machine, I need to get decimal separator character from a C++ program.要使用正确的数字分隔符('.' 或 ',')生成 csv 文件,因为我希望它们与机器上安装的 Excel 版本兼容,我需要从 C++ 程序中获取小数点分隔符。
My machine has a French version of Windows/Excel, so decimal separator is ','.我的机器有法语版的 Windows/Excel,所以小数点分隔符是 ','。
int main()
{
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
return 0;
}
outputs .
输出
.
, which is not expected ,这是意料之中的
I tried using WIN32 API:我尝试使用 WIN32 API:
int main()
{
TCHAR szSep[8];
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, szSep, 8);
std::cout << szSep;
}
outputs ,
, which is expected.输出
,
,这是预期的。
Is there any equivalent to this GetLocaleInfo
function in STL that will work inside a simple main
?是否有任何等效于 STL 中的
GetLocaleInfo
函数可以在简单的main
?
Thanks to user0042 linked example, the appropriate way to do this using STL is:感谢 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 ,
, which is expected.输出
,
,这是预期的。
Or, if you don't want to change the global:或者,如果您不想更改全局:
int main()
{
std::cout.imbue(std::locale(""));
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.