如何更改默认语言环境的千位分隔符?

Mar*_* Ba 5 c++ locale iostream visual-c++

我可以

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

它会输出:

i: 123.456 f: 3,14
Run Code Online (Sandbox Code Playgroud)

在我的系统上.(德国窗户)

我想避免获得成千上万的分隔符 - 我怎么能这样做?

(我只想要用户默认设置,但没有任何千位分隔符.)

(我所发现的是如何读取使用千位分隔符use_facetnumpunct小......但我要如何改变呢?)

Ano*_*ard 9

只需创建并灌输您自己的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";
}
Run Code Online (Sandbox Code Playgroud)

如果您必须为另一个要读取的应用程序创建特定输出,您可能还想覆盖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";
}
Run Code Online (Sandbox Code Playgroud)