C/C++: printf use commas instead of dots as decimal separator

眉间皱痕 提交于 2019-12-13 13:28:32

问题


I need that my program shows the output using commas as decimal separator.

According to this link: http://www.java2s.com/Code/Java/Development-Class/Floatingpointwithcommasf.htm you can use in Java something like this:

System.out.printf("Floating-point with commas: %,f\n", 1234567.123);

Is there a flag or something that I can use to have a similar behaviour in C?

Thanks in advance!


回答1:


If you want, you can set current locale at the beginning of your program:

#include <stdio.h>
#include <locale.h>


int main()
{
    setlocale(LC_NUMERIC, "French_Canada.1252"); // ".OCP" if you want to use system settings
    printf("%f\n", 3.14543);
    return 0;
}



回答2:


There is no similar functionality in C. You can use sprintf to print into a char array and then replace the dot with a comma manually. I can't think of a better solution, sorry.

EDIT: thanks to Mats Patersson's comment: It seems setting the locale can change this character. Please have a look at the link he posted.



来源:https://stackoverflow.com/questions/14753505/c-c-printf-use-commas-instead-of-dots-as-decimal-separator

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