fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. unsigned long long ConstructMoney(unsigned long long dollars, unsigned cents)
  5. {
  6. return dollars * 100 + cents;
  7. }
  8.  
  9. void PrintWithCommas(unsigned long long n)
  10. {
  11. char s[sizeof n * CHAR_BIT + 1];
  12. char *p = s + sizeof s;
  13. unsigned count = 0;
  14. *--p = '\0';
  15. do
  16. {
  17. *--p = '0' + n % 10;
  18. n /= 10;
  19. if (++count == 3 && n)
  20. {
  21. *--p = ',';
  22. count = 0;
  23. }
  24. } while (n);
  25. printf("%s", p);
  26. }
  27.  
  28. void PrintMoney(unsigned long long n)
  29. {
  30. PrintWithCommas(n / 100);
  31. putchar('.');
  32. n %= 100;
  33. putchar('0' + n / 10);
  34. putchar('0' + n % 10);
  35. }
  36.  
  37. int main(void)
  38. {
  39. PrintMoney(ConstructMoney(0, 0)); puts("");
  40. PrintMoney(ConstructMoney(0, 1)); puts("");
  41. PrintMoney(ConstructMoney(1, 0)); puts("");
  42. PrintMoney(ConstructMoney(1, 23)); puts("");
  43. PrintMoney(ConstructMoney(12, 34)); puts("");
  44. PrintMoney(ConstructMoney(123, 45)); puts("");
  45. PrintMoney(ConstructMoney(1234, 56)); puts("");
  46. PrintMoney(ConstructMoney(12345, 67)); puts("");
  47. PrintMoney(ConstructMoney(123456, 78)); puts("");
  48. PrintMoney(ConstructMoney(1234567, 89)); puts("");
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
0.00
0.01
1.00
1.23
12.34
123.45
1,234.56
12,345.67
123,456.78
1,234,567.89