C / C ++:printf使用逗号而不是点作为小数点分隔符
问题描述:
我需要我的程序使用逗号作为小数点分隔符来显示输出。
I need that my program shows the output using commas as decimal separator.
根据此链接:可以在Java中使用的http://www.java2s.com/Code/Java/Development-Class/Floatingpointwithcommasf.htm 像这样的东西:
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);
在C中是否存在标志或可用于类似行为的东西?
Is there a flag or something that I can use to have a similar behaviour in C?
谢谢!
答
如果需要,可以设置当前语言环境在程序开始时:
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;
}