How to change the default thousands separator of my locale?

I can do

locale loc(""); // use default locale cout.imbue( loc ); cout << << "i: " << int(123456) << " f: " << float(3.14) << "\n"; 

and he 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 need the default settings for users, but without the thousands separator.)

(All I found is how to read the thousands separator using use_facet with the numpunct face ... but how do I change it?)

+4
source share
1 answer

Just create and fill your own numpunct face:

 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 need to create specific output for reading another application, you can also override virtual char_type numpunct::do_decimal_point() const; .

If you want to use a specific locale as a base, you can get faces from _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"; } 
+8
source

Source: https://habr.com/ru/post/1446523/


All Articles