home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STPMONEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  4.1 KB  |  140 lines

  1. /**
  2. *
  3. * Name        stpmoney -- Format money amount in national style
  4. *
  5. * Synopsis    presult = stpmoney(ptarget,amount,yes_sym,tarsize,pinfo);
  6. *
  7. *        char   *presult The resulting string; or NIL if the
  8. *                string would have exceeded tarsize.
  9. *        char   *ptarget Buffer in which to put the resulting string
  10. *        double amount    The monetary amount
  11. *        int    yes_sym    Zero if currency symbol not to be included,
  12. *                nonzero if symbol to be included.
  13. *        int    tarsize    Capacity of target buffer, including one
  14. *                space for the trailing NUL.
  15. *        COUNTRY_INFO *pinfo
  16. *                Pointer to structure with country info
  17. *
  18. * Description    This function formats an amount of money according to
  19. *        DOS's information about national style.  pinfo must
  20. *        point to a COUNTRY_INFO structure in the format returned
  21. *        by QYGCOUN:  in particular, the csym, thsep, decsep,
  22. *        cfmt, and cprec members of the structure must be valid.
  23. *        (This may not be true if the structure was filled by DOS
  24. *        2.x, or if QYGCOUN reported an error.)
  25. *
  26. *        The currency symbol (such as '$' for the United States)
  27. *        will be included if yes_sym is nonzero.  If used, the
  28. *        currency symbol will be added at the conventional
  29. *        location (possibly before or after the amount and
  30. *        possibly separated from the amount by a blank).
  31. *
  32. *        Fractional amounts are formatted according to the cprec
  33. *        member of the *pinfo structure.  Roundoff is not
  34. *        performed:  missing digits are truncated.
  35. *
  36. * Returns    presult     The resulting string; or NIL if the
  37. *                string would have exceeded the target
  38. *                string capacity.
  39. *        *ptarget    The resulting string.
  40. *
  41. * Version    3.0 (C)Copyright Blaise Computing Inc.    1986
  42. *
  43. **/
  44.  
  45. #include <math.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49.  
  50. #include <bstring.h>
  51.  
  52. char *stpmoney(ptarget,amount,yes_sym,tarsize,pinfo)
  53. register char          *ptarget;
  54. double              amount;
  55. int              yes_sym;
  56. register COUNTRY_INFO *pinfo;
  57. register int          tarsize;
  58. {
  59.     char *save_target,*amtbuf,*pcurr_sym,thou_sep;
  60.     int  decptr,sign,precision;
  61.  
  62.     /* First deduct the non-numeric characters from tarsize.          */
  63.  
  64.     tarsize--;                    /* Trailing NUL          */
  65.     if (yes_sym)
  66.     {
  67.     tarsize -= (int) strlen(pinfo->csym);  /* Currency symbol     */
  68.     if ((pinfo->cfmt) & 2)
  69.         tarsize--;                /* Intervening blank      */
  70.     }
  71.     if (precision = (int) (pinfo->cprec))
  72.     tarsize--;                /* Decimal separator      */
  73.  
  74.     /* Next measure whether the amount will fit in the remaining      */
  75.     /* space.                                  */
  76.  
  77.     amtbuf = ecvt(amount,tarsize + 1,&decptr,&sign);
  78.     if (sign)
  79.     tarsize--;                /* Minus sign          */
  80.     if (thou_sep = (pinfo->thsep)[0])
  81.     tarsize -= (max(1,(decptr-1))) / 3; /* Thousands separator(s) */
  82.     if ((max(decptr,1) + precision) > tarsize)
  83.     return NIL;
  84.  
  85.     /* Create the result piece by piece:  first (possibly) the          */
  86.     /* the currency symbol and intervening blank.              */
  87.  
  88.     save_target = ptarget;
  89.     if (yes_sym && (((pinfo->cfmt) & 1) == 0))
  90.     {
  91.     for (pcurr_sym = (pinfo->csym); *pcurr_sym; pcurr_sym++)
  92.         *ptarget++ = *pcurr_sym;
  93.     if ((pinfo->cfmt) & 2)
  94.         *ptarget++ = ' ';
  95.     }
  96.  
  97.     /* Next, the sign (if any).                       */
  98.  
  99.     if (sign)
  100.     *ptarget++ = '-';
  101.  
  102.     /* Next, the digits preceding the decimal separator, along with   */
  103.     /* any thousands separators.                      */
  104.  
  105.     if (decptr > 0)
  106.     while (decptr--)
  107.     {
  108.         *ptarget++ = *amtbuf++;
  109.         if ((decptr > 0) && ((decptr % 3) == 0))
  110.         if (thou_sep)
  111.             *ptarget++ = thou_sep;
  112.     }
  113.     else
  114.     *ptarget++ = '0';
  115.  
  116.     /* If the format calls for a decimal fraction, add the decimal    */
  117.     /* separator now.                              */
  118.  
  119.     if (precision)
  120.     *ptarget++ = (pinfo->decsep)[0];
  121.  
  122.     /* Now copy the fractional part.                      */
  123.  
  124.     while (precision--)
  125.     *ptarget++ = *amtbuf++;
  126.  
  127.     /* Finish with the intervening blank and currency symbol.          */
  128.  
  129.     if (yes_sym && (((pinfo->cfmt) & 1) != 0))
  130.     {
  131.     if ((pinfo->cfmt) & 2)
  132.         *ptarget++ = ' ';
  133.     for (pcurr_sym = (pinfo->csym); *pcurr_sym; pcurr_sym++)
  134.         *ptarget++ = *pcurr_sym;
  135.     }
  136.     *ptarget = '\0';
  137.  
  138.     return save_target;
  139. }
  140.