问题
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