fork download
  1. #include <locale>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. class comma_numpunct : public std::numpunct<char>
  7. {
  8. public:
  9. comma_numpunct(char thousands_sep, const char* grouping)
  10. :m_thousands_sep(thousands_sep),
  11. m_grouping(grouping){}
  12. protected:
  13. char do_thousands_sep() const{return m_thousands_sep;}
  14. std::string do_grouping() const {return m_grouping;}
  15. private:
  16. char m_thousands_sep;
  17. std::string m_grouping;
  18. };
  19.  
  20. int main()
  21. {
  22.  
  23. std::locale comma_locale(std::locale(), new comma_numpunct(',', "\03"));
  24.  
  25. std::cout.imbue(comma_locale);
  26. std::cout << std::setprecision(7) << std::fixed << 1000000.1234;
  27. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
1,000,000.1234000