home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FMTMONEY.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  126 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FMTMONEY.C - Format a U.S. dollar value into a numeric string
  5. **
  6. **  public domain demo by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include "snipmath.h"
  14.  
  15. #define Form(s,a) bufptr += sprintf(bufptr, s, a)
  16.  
  17. static char buf[256], *bufptr;
  18.  
  19. static char *units[] = {"Zero", "One", "Two", "Three", "Four",
  20.                         "Five", "Six", "Seven", "Eight", "Nine",
  21.                         "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
  22.                         "Fifteen", "Sixteen", "Seventeen", "Eighteen",
  23.                         "Nineteen"},
  24.  
  25.             *tens[]  = {"Twenty", "Thirty", "Forty", "Fifty", "Sixty",
  26.                         "Seventy", "Eighty", "Ninety"};
  27.  
  28. static void form_group(int, char *);
  29.  
  30. /*
  31. **  Call with double amount
  32. **  Rounds cents
  33. **  Returns string in a static buffer
  34. */
  35.  
  36. char *fmt_money(double amt)
  37. {
  38.       int temp;
  39.       double dummy, cents = modf(amt, &dummy);
  40.  
  41.       *buf = '\0';
  42.       bufptr = buf;
  43.  
  44.       temp = (int)(amt/1E12);
  45.       if (temp)
  46.       {
  47.             form_group(temp, "Trillion");
  48.             amt = fmod(amt, 1E12);
  49.       }
  50.  
  51.       temp = (int)(amt/1E9);
  52.       if (temp)
  53.       {
  54.             form_group(temp, "Billion");
  55.             amt = fmod(amt, 1E9);
  56.       }
  57.  
  58.       temp = (int)(amt/1E6);
  59.       if (temp)
  60.       {
  61.             form_group(temp, "Million");
  62.             amt = fmod(amt, 1E6);
  63.       }
  64.  
  65.       temp = (int)(amt/1E3);
  66.       if (temp)
  67.       {
  68.             form_group(temp, "Thousand");
  69.             amt = fmod(amt, 1E3);
  70.       }
  71.       form_group((int)amt, "");
  72.  
  73.       if (buf == bufptr)
  74.             Form("%s ", units[0]);
  75.  
  76.       temp = (int)(cents * 100. + .5);
  77.       sprintf(bufptr, "& %02d/100", temp);
  78.  
  79.       return buf;
  80. }
  81.  
  82. /*
  83. **  Process each thousands group
  84. */
  85.  
  86. static void form_group(int amt, char *scale)
  87. {
  88.       if (buf != bufptr)
  89.             *bufptr++ = ' ';
  90.  
  91.       if (100 <= amt)
  92.       {
  93.             Form("%s Hundred ", units[amt/100]);
  94.             amt %= 100;
  95.       }
  96.       if (20 <= amt)
  97.       {
  98.             Form("%s", tens[(amt - 20)/10]);
  99.             if (0 != (amt %= 10))
  100.             {
  101.                   Form("-%s ", units[amt]);
  102.             }
  103.                 else    Form("%s", " ");
  104.       }
  105.       else if (amt)
  106.       {
  107.             Form("%s ", units[amt]);
  108.       }
  109.  
  110.       Form("%s", scale);
  111. }
  112.  
  113. #ifdef TEST
  114.  
  115. main(int argc, char *argv[])
  116. {
  117.       while (--argc)
  118.       {
  119.             double amt = atof(*(++argv));
  120.             printf("fmt_money(%g) = %s\n", amt, fmt_money(amt));
  121.       }
  122.       return EXIT_SUCCESS;
  123. }
  124.  
  125. #endif /* TEST */
  126.