2

I am looking for a nice way to print large numbers so they are more readable

ie 6000000

should be

6.000.000

OR

6,000,000 depending on locale

UPDATE

I have tried the following on my code (its on IOS)

char* localeSet = std::setlocale(LC_ALL, "en_US");
cout << "LOCALE AFTER :" << std::locale("").name() << endl;

localeSet is always NILL

and I always get "LCOALE AFTER: C"

Middy
  • 91
  • 1
  • 9

1 Answers1

0

In std C++ something like this:

template < class T >
std::string Format( T number )
{
    std::stringstream ss;
    ss << number;
    const std::string num = ss.str();
    std::string result;
    const size_t size = num.size();
    for ( size_t i = 0; i < size; ++i )
    {
        if ( i % 3 == 0 && i != 0  )
            result = '.' + result;
        result = ( num[ size - 1 - i ] + result );
    }
    return result;
}

...
long x = 1234567;
std::cout << Format( x ) << std::endl;
...
Daniele Pallastrelli
  • 2,430
  • 1
  • 21
  • 37
  • Have you looked at the question linked in the comments, which was there long before you posted your answer? Why don't you use the stringstream in the loop, or why are indices better then iterators/streaming operations? Why are you copying `result` over and over instead of using `push_back()`? – Sebastian Mach Jan 16 '13 at 09:00
  • Also: Your code is buggy for some negative numbers, e.g. `-100000 -> -.100.000`. – Sebastian Mach Jan 16 '13 at 09:11