【发布时间】:2021-11-20 09:20:05
【问题描述】:
我试图让我的输出编号在超过 1,000 时具有标点符号。 我搜索了一些使用它的函数。
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;
struct numberPunctuations : numpunct<char> {
string do_grouping() const {
return "\03";
}
};
int main()
{
locale loc (cout.getloc(), new numberPunctuations);
cout.imbue(loc);
cout<<1000;
return 0;
}
它可以工作,但我看到了一个较短的代码,但我无法让它工作。
#include <iostream>
#include <iomanip>
#include <locale>
using namespace std;
int main()
{
cout<<fixed<< setprecision(2); // decimal place
// comma separator
locale loc ("en_US.UTF-8");
cout.imbue(loc);
//cout.imbue(locale("en_US.UTF-8"));
// test data
cout<<123425 + 342346<<'\n';
cout<<123.523456;
return 0;
}
当我尝试运行它时,没有帖子(没有输出),但是当我使用 vim 和在线编译器 (OnlineGDB) 时,它可以工作。我喜欢第二个代码,因为它更短,有没有办法让它工作?
PS:我使用的IDE是clion(学生版)和VS code。
【问题讨论】:
-
您在 Windows 上吗?语言环境
std_name只有几个标准化的名称(或者可能只有一个,C)。不幸的是,en_US.UTF-8没有标准化。 -
是的,我在 Windows 上,我试过 C.UTF-8 它也不起作用当我删除 UTF-8 时有输出但标点符号也不起作用
-
在 Windows 上,区域设置只是
.UTF8,名称中没有C。这是因为C语言环境始终是系统的 ANSI 代码页,因此它实际上是在请求某些内容,然后同时请求其他内容。 -
您确定要对语言环境进行硬编码吗?不会选择用户的语言环境并进行设置吗?
-
@TedLyngmo 如果他们只是做了
".UTF8",那将是带有用户语言环境的utf-8。
标签: c++ locale number-formatting punctuation numberformatter