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

给你一囗甜甜゛ 提交于 2019-11-27 17:36:18

问题


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,50 instead of 1.50.

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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/7684014/c-printf-with-f-but-localized-for-the-users-country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!