开发者

C++ printf with %f but localized for the user's country

开发者 https://www.devze.com 2023-04-11 03:45 出处:网络
I\'m using the following C++ syntax to output a floating point value on a Windows platform: printf(\"%.2f\", 1.5);

I'm using the following C++ syntax to output a floating point value on a Windows platform:

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,5开发者_JAVA百科0 instead of 1.50.

Why do I not see it and how to produce my desired result?


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.

You can set the current locale for a C/C++ program using the setlocale function.

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


Try using setlocale() function http://www.cplusplus.com/reference/clibrary/clocale/setlocale/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号