Yes, this can be done automatically in C ++ by setting the correct face in the locale.
#include <iostream> #include <locale> #include <string> template<typename CharT> struct Sep : public std::numpunct<CharT> { virtual std::string do_grouping() const {return "\003";} }; int main() { std::cout.imbue(std::locale(std::cout.getloc(), new Sep <char>())); std::cout << 123456789 << "\n"; }
Note. C-locale (the locale used when your application is not specifically configured) does not use the thousands separator. If you set the language standard of your application to a specific language, then it will select the language that corresponds to the grouping method (without having to do something like the above). If you want to set the locale to what your current language settings are for your computer (as defined by the OS), and not a specific language, then use "" (blank line) as the locale.
So, to set the locale based on the specific settings of your OS:
int main() { std::cout.imbue(std::locale("")); std::cout << 123456789 << "\n"; }
Loki Astari Aug 13 '10 at 18:01 2010-08-13 18:01
source share