Inserting and removing commas from integers in c++

后端 未结 1 388
生来不讨喜
生来不讨喜 2021-01-02 21:25

Very much a noob here, so it\'s best to assume I know nothing in any answers.

I\'ve been writing a little app, and it\'s working well, but readability is a nightmare

相关标签:
1条回答
  • 2021-01-02 22:10

    Try the numpunct facet and overload the do_thousands_sep function. There is an example. I also hacked up something that just solves your problem:

    #include <locale>
    #include <iostream>
    
    class my_numpunct: public std::numpunct<char> {
        std::string do_grouping() const { return "\3"; }
    }; 
    
    int main() {
        std::locale nl(std::locale(), new my_numpunct); 
        std::cout.imbue(nl);
        std::cout << 1000000 << "\n"; // does not use thousands' separators
        std::cout.imbue(std::locale());
        std::cout << 1000000 << "\n"; // uses thousands' separators
    } 
    
    0 讨论(0)
提交回复
热议问题