[ create a new paste ] login | about

Link: http://codepad.org/In3b4ATI    [ raw code | output | fork ]

ninwa - C++, pasted on Nov 29:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string nice_number(long n)
{
	bool negative = (n < 0 ) ? true : false;
	
	stringstream s;
	s << ((negative) ? -n : n);
	
	if( s.str().length() < 4 ){
		stringstream s;
		s << n;
		return s.str();
	}

	string number = "";
	unsigned i = s.str().length();
	
	while( i-- != 0 ){
		if( (s.str().length() - i) % 3 == 0 && i != s.str().length()-1 )
			number = string(",") + s.str().at(i) + number;
		else
			number = s.str().at(i) + number;
	}
    
	return (negative) ? string("-") + number : number;
}

int main( int argc, char **argv )
{
	cout << nice_number(-13123234) << endl;	
}


Output:
1
-13,123,234


Create a new paste based on this one


Comments: