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?)
Click File > Options. On the Advanced tab, under Editing options, clear the Use system separators check box. Type new separators in the Decimal separator and Thousands separator boxes.
Windows 10 - click on Start, start typing “Control Panel”, select it, and go to Region. Click Additional Settings. For “Decimal Symbol”, enter a full stop ( . ). For “List Separator”, enter a comma ( , ).
The decimal separator is also called the radix character. Likewise, while the U.K. and U.S. use a comma to separate groups of thousands, many other countries use a period instead, and some countries separate thousands groups with a thin space.
The character used as the thousands separatorIn the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With