windows 带有 %f 但针对用户所在国家/地区进行了本地化的 C++ printf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7684014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ printf with %f but localized for the user's country
提问by ahmd0
I'm using the following C++ syntax to output a floating point value on a Windows platform:
我正在使用以下 C++ 语法在 Windows 平台上输出浮点值:
printf("%.2f", 1.5);
It works well if I run it on an English user account. My assumption was that if I run it on, say French user account, the output will be 1,50 instead of 1.50.
如果我在英文用户帐户上运行它,效果会很好。我的假设是,如果我在法国用户帐户上运行它,输出将是 1,50 而不是 1.50。
Why do I not see it and how to produce my desired result?
为什么我看不到它以及如何产生我想要的结果?
回答by MartinStettner
The radix character (i.e. '.' or ',') is defined by the current locale. The default locale (at least for Windows systems) is "C", which defines '.' as radix character.
基数字符(即“.”或“,”)由当前语言环境定义。默认语言环境(至少对于 Windows 系统)是“C”,它定义了 '.' 作为基数字符。
You can set the current locale for a C/C++ program using the setlocale
function.
您可以使用该setlocale
函数为 C/C++ 程序设置当前语言环境。
To set the locale to the current system/user locale, you can use the following statement:
要将语言环境设置为当前系统/用户语言环境,可以使用以下语句:
#include <locale.h>
setlocale(LC_ALL, ".OCP");
See here(cf. the examples on the linked page...) for more information about setlocale
有关更多信息,请参见此处(参见链接页面上的示例...)setlocale
回答by Damian Walczak
Try using setlocale() function http://www.cplusplus.com/reference/clibrary/clocale/setlocale/
尝试使用 setlocale() 函数 http://www.cplusplus.com/reference/clibrary/clocale/setlocale/