要使用正确的数字分隔符('.' 或 ',')生成 csv 文件,因为我希望它们与机器上安装的 Excel 版本兼容,我需要从 C++ 程序中获取小数分隔符。
我的机器有法语版的 Windows/Excel,所以小数点分隔符是 ','。
int main()
{
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出.,这是不期望的
我尝试使用 WIN32 API:
int main()
{
TCHAR szSep[8];
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, szSep, 8);
std::cout << szSep;
}
Run Code Online (Sandbox Code Playgroud)
输出,,这是预期的。
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();
}
Run Code Online (Sandbox Code Playgroud)
输出,,这是预期的。
或者,如果您不想更改全局:
int main()
{
std::cout.imbue(std::locale(""));
std::cout << std::use_facet< std::numpunct<char> >(std::cout.getloc()).decimal_point();
}
Run Code Online (Sandbox Code Playgroud)