函数localeconv() 只是读取定位设置,ptrLocale->thousands_sep 本身不会更改当前语言环境的设置。
编辑:
我不知道如何在 C 中执行此操作,但可以找到很多带有 C++ 输出的示例。
请参阅以下 C++ 示例:
#include <iostream>
#include <locale>
using namespace std;
struct myseps : numpunct<char> {
// use ' as separator
char do_thousands_sep() const { return '\''; }
// digits are grouped by 3
string do_grouping() const { return "\3"; }
};
int main() {
cout.imbue(locale(locale(), new myseps));
cout << 1234567; // the result will be 1'234'567
}
编辑 2:
C++ 参考说:
localeconv() 返回一个指向 struct lconv 类型的填充对象的指针。对象中包含的值可以被后续调用 localeconv 覆盖,并且不会直接修改对象。使用类别值为 LC_ALL、LC_MONETARY 或 LC_NUMERIC 调用 setlocale 会覆盖结构的内容。
我在 MS Visual Studio 2012 中尝试了以下示例(我知道这是不好且不安全的样式):
#include <stdio.h>
#include <locale.h>
#include <string.h>
int main() {
setlocale(LC_NUMERIC, "");
struct lconv *ptrLocale = localeconv();
strcpy(ptrLocale->decimal_point, ":");
strcpy(ptrLocale->thousands_sep, "'");
char str[20];
printf("%10.3lf \n", 13000.26);
return 0;
}
我看到了结果:
13000:260
因此,可以假设decimal_point和thousands_sep的变化可以通过localeconv()接收到的指针来实现,但是printf忽略了thousands_sep。
编辑 3:
更新的 C++ 示例:
#include <iostream>
#include <locale>
#include <sstream>
using namespace std;
struct myseps : numpunct<char> {
// use ' as separator
char do_thousands_sep() const { return '\''; }
// digits are grouped by 3
string do_grouping() const { return "\3"; }
};
int main() {
stringstream ss;
ss.imbue(locale(locale(), new myseps));
ss << 1234567; // printing to string stream with formating
printf("%s\n", ss.str().c_str()); // just output when ss.str() provide string, and c_str() converts it to char*
}