###You should not do this manually.
You should not do this manually.
The local do this for you correctly for the locale you are currently in (with the correct separation character for the locale).
The problem is that by default your code runs in the "C" locale. This uses no separators. But you can make your code pick up the local used by the current machine.
#include <iostream>
#include <locale>
#include <string>
int main()
{
std::cout.imbue(std::locale(""));
std::cout << 123456789 << "\n";
}
The output of this is:
123,456,789
In Germany it would look like:
123 456.789 // apparently correct separation for DE
Alternatively if you want your own specific separation you can set up the locale object on the stream to do the work specific to your application.
#include <iostream>
#include <locale>
#include <string>
template<typename CharT>
struct Sep : public std::numpunct<CharT>
{
virtual std::string do_grouping() const {return "\003";}
virtual CharT do_thousands_sep() const {return ':';}
};
int main()
{
std::cout.imbue(std::locale(std::cout.getloc(), new Sep <char>()));
std::cout << 123456789 << "\n"; // this prints 123:456:789
}
This outputs:
123:456:789