home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / FMTMONEY.C < prev    next >
C/C++ Source or Header  |  1991-12-02  |  3KB  |  120 lines

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