How to print a number with a space as thousand separator?

Posted by dygi on Stack Overflow See other posts from Stack Overflow or by dygi
Published on 2010-04-15T19:29:08Z Indexed on 2010/04/15 19:33 UTC
Read the original article Hit count: 417

Filed under:

I've got a simple class Currency with overloaded operator<<. I don't know how can i separate the number with spaces every 3 digits, so it looks like: "1 234 567 ISK".

#include <cstdlib>
#include <iostream>

using namespace std;

class Currency
{
    int val;
    char curr[4];

    public:
    Currency(int _val, const char * _curr)
    {
        val = _val;
        strcpy(curr, _curr);
    }

    friend ostream & operator<< (ostream & out, const Currency & c);
};

ostream & operator<< (ostream & out, const Currency & c)
{
    out << c.val<< " " << c.curr;
    return out;
}

int main(int argc, char *argv[])
{
    Currency c(2354123, "ISK");
    cout << c;
}

What interests me, is somehow the easiest solution for this particular situation.

© Stack Overflow or respective owner

Related posts about c++