Syntax for printing commas in numerics

I've got a program I need a mortgage table printed as an output and I'm struggling with the syntax for printing commas in the digits(ex. 20,000 instead of 20000).

1
2
3
4
5
6
cin >> prin;

if (prin == 20,000)
{
     cout << myTable << endl;
}


Not only do i need it to print in the table will it matter if its the user input for prin? Thanks for any and all help!

So, you are trying to insert commas into the numbers, then print them?
Typically you need to imbue the stream with a locale that automatically does that for you.

For example, on Linux, my locale is en_US.UTF-8.

If I do:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <locale>

int main()
{
    const int x = 1000000;
    std::cout.imbue(std::locale(""));
    std::cout << x << std::endl;
    return 0;
}


The nice thing about this is that it formats the number properly for whatever locale is appropriate for the user.

I get:

$ ./locale_test 
1,000,000


A German might see this:

$ LANG=de_DE.UTF-8 ./locale_test 
1.000.000
Last edited on
That works fine on Linux, but locale support is typically broken on Windows. MinGW users must use STLPort or some other standard library to get C++ locale support of any kind.
The code works just fine with VS2010.

Topic archived. No new replies allowed.