How do I change the thousands separator of my default locale?
c++, iostream, locale, visual-c++
Solution
Just create and imbue your own numpunct facet:
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";
}
If you have to create a specific output for another application to read, you may also want to override `virtual char_type numpunct::do_decimal_point() const;`.
If you want to use a specific locale as base, you can derive from the `_byname` facets:
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";
}
Problem
I can do ``` locale loc(""); // use default locale cout.imbue( loc ); cout << << "i: " << int(123456) << " f: " << float(3.14) << "\n"; ``` and it will output: ``` i: 123.456 f: 3,14 ``` on my system. (german windows) I would like to avoid getting the thousands separator for ints -- how can I do this? (I just want the users default settings but without any thousands separator.) (All I found is how to read the thousands separator using `use_facet` with the `numpunct` facet ... but how do I change it?)