Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the thousands separator of my default locale?

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?)

like image 652
Martin Ba Avatar asked Nov 16 '12 19:11

Martin Ba


People also ask

How do you change a thousand separator?

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.

How do I change system separators in Windows 10?

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 ( , ).

Which countries use comma as thousand separator?

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.

What is the thousand separator format?

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.


1 Answers

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";
}
like image 150
Anonymous Coward Avatar answered Oct 31 '22 17:10

Anonymous Coward