Skip to main content
Commonmark migration
Source Link

###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

###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

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
Spelling fixes
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

You should not do this manually ###You should not do this manually.

The local do this for you correctly for the locallocale 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 seporatorsseparators. 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

You should not do this manually.

The local do this for you correctly for the local 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 seporators. 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

###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
Source Link
Loki Astari
  • 96.6k
  • 5
  • 125
  • 338

You should not do this manually.

The local do this for you correctly for the local 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 seporators. 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