我可以
locale loc(""); // use default locale
cout.imbue( loc );
cout << << "i: " << int(123456) << " f: " << float(3.14) << "\n";
它会输出:
i: 123.456 f: 3,14
在我的系统上。 (德语窗口)
我想避免获取整数的千位分隔符——我该怎么做?
(我只想要用户默认设置,但没有任何千位分隔符。)
(我 found 的全部内容是如何使用 use_facet
和 numpunct
facet 来读取千位分隔符...但是我该如何更改它?)
最佳答案
只需创建并注入(inject)您自己的 numpunct 方面:
struct no_separator : std::numpunct<char> {
protected:
virtual string_type do_grouping() const
{ return "\000"; } // groups of 0 (disable)
};
int main() {
locale loc("");
// imbue loc and add your own facet:
cout.imbue( locale(loc, new no_separator()) );
cout << "i: " << int(123456) << " f: " << float(3.14) << "\n";
}
如果您必须为另一个应用程序创建特定的输出以供读取,您可能还需要覆盖 virtual char_type numpunct::do_decimal_point() const;
。
如果你想使用特定的语言环境作为基础,你可以从 _byname
方面派生:
template <class charT>
struct no_separator : public std::numpunct_byname<charT> {
explicit no_separator(const char* name, size_t refs=0)
: std::numpunct_byname<charT>(name,refs) {}
protected:
virtual string_type do_grouping() const
{ return "\000"; } // groups of 0 (disable)
};
int main() {
cout.imbue( locale(std::locale(""), // use default locale
// create no_separator facet based on german locale
new no_separator<char>("German_germany")) );
cout << "i: " << int(123456) << " f: " << float(3.14) << "\n";
}
关于c++ - 如何更改默认语言环境的千位分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13422892/