C语言 MinGW and setlocale



我试图将千位分隔符设置为'。'或空格和小数分隔符为','。

我在Windows 10上使用gcc.exe (MinGW-W64 x86_64-posix-seh,由Brecht Sanders构建)12.1.0。

当我尝试编译这段代码时:

#include <stdio.h>
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "French");
int a = 1000000;
float b = 1.10F;
printf("%'d.n", a);
printf("%'g.n", b);

return 0;
}

输出:1000000年

1, 1 .

你知道怎么了吗?

顺便说一下setlocale(LC_ALL, "fr-FR");没有任何效果。

提前感谢您的帮助。

在Windows上,使用目标为x86_64-w64-mingw32的gcc.exe,下面的代码将输出"3,141593"。然而,在以x86_64-pc-msys为目标的gcc.exe编译器上,代码将输出"3.141593"。在我看来,Windows实际上更关心区域设置。我认为你应该切换到使用MinGW,或者,如果这不是一个选项,短跑到一个变量,并取代最后一个'。' with ','和除最后一个'之外的所有',' with '.'.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#define PI 3.14159265
int main(void)
{
setlocale(LC_NUMERIC, "French");
printf("%fn", PI);
return EXIT_SUCCESS;
}
编辑:对不起,我误解了这个问题。我真的不知道thousand分隔符是如何与语言环境交互的——所以我唯一能提供的就是一个用逗号替换点,用点替换逗号的函数。请注意,您需要使用类似sprintf的东西将您的数字转换为字符串才能工作。
void to_european_format(char * string)
{
size_t i = 0;
while (string[i] != '')
{
if (string[i] == '.')
string[i] = ',';
else if (string[i] == ',')
string[i] = '.';
i++;
}
return;
}

最新更新