fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. string commify(unsigned long long n)
  7. {
  8. string s;
  9. int cnt = 0;
  10. do
  11. {
  12. s.insert(0, 1, char('0' + n % 10));
  13. n /= 10;
  14. if (++cnt == 3 && n)
  15. {
  16. s.insert(0, 1, ',');
  17. cnt = 0;
  18. }
  19. } while (n);
  20. return s;
  21. }
  22.  
  23. int main()
  24. {
  25. cout << commify(0) << endl;
  26. cout << commify(1) << endl;
  27. cout << commify(999) << endl;
  28. cout << commify(1000) << endl;
  29. cout << commify(1000000) << endl;
  30. cout << commify(1234567890ULL) << endl;
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
0
1
999
1,000
1,000,000
1,234,567,890