home *** CD-ROM | disk | FTP | other *** search
- /**
- *
- * Name stpmoney -- Format money amount in national style
- *
- * Synopsis presult = stpmoney(ptarget,amount,yes_sym,tarsize,pinfo);
- *
- * char *presult The resulting string; or NIL if the
- * string would have exceeded tarsize.
- * char *ptarget Buffer in which to put the resulting string
- * double amount The monetary amount
- * int yes_sym Zero if currency symbol not to be included,
- * nonzero if symbol to be included.
- * int tarsize Capacity of target buffer, including one
- * space for the trailing NUL.
- * COUNTRY_INFO *pinfo
- * Pointer to structure with country info
- *
- * Description This function formats an amount of money according to
- * DOS's information about national style. pinfo must
- * point to a COUNTRY_INFO structure in the format returned
- * by QYGCOUN: in particular, the csym, thsep, decsep,
- * cfmt, and cprec members of the structure must be valid.
- * (This may not be true if the structure was filled by DOS
- * 2.x, or if QYGCOUN reported an error.)
- *
- * The currency symbol (such as '$' for the United States)
- * will be included if yes_sym is nonzero. If used, the
- * currency symbol will be added at the conventional
- * location (possibly before or after the amount and
- * possibly separated from the amount by a blank).
- *
- * Fractional amounts are formatted according to the cprec
- * member of the *pinfo structure. Roundoff is not
- * performed: missing digits are truncated.
- *
- * Returns presult The resulting string; or NIL if the
- * string would have exceeded the target
- * string capacity.
- * *ptarget The resulting string.
- *
- * Version 3.0 (C)Copyright Blaise Computing Inc. 1986
- *
- **/
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #include <bstring.h>
-
- char *stpmoney(ptarget,amount,yes_sym,tarsize,pinfo)
- register char *ptarget;
- double amount;
- int yes_sym;
- register COUNTRY_INFO *pinfo;
- register int tarsize;
- {
- char *save_target,*amtbuf,*pcurr_sym,thou_sep;
- int decptr,sign,precision;
-
- /* First deduct the non-numeric characters from tarsize. */
-
- tarsize--; /* Trailing NUL */
- if (yes_sym)
- {
- tarsize -= (int) strlen(pinfo->csym); /* Currency symbol */
- if ((pinfo->cfmt) & 2)
- tarsize--; /* Intervening blank */
- }
- if (precision = (int) (pinfo->cprec))
- tarsize--; /* Decimal separator */
-
- /* Next measure whether the amount will fit in the remaining */
- /* space. */
-
- amtbuf = ecvt(amount,tarsize + 1,&decptr,&sign);
- if (sign)
- tarsize--; /* Minus sign */
- if (thou_sep = (pinfo->thsep)[0])
- tarsize -= (max(1,(decptr-1))) / 3; /* Thousands separator(s) */
- if ((max(decptr,1) + precision) > tarsize)
- return NIL;
-
- /* Create the result piece by piece: first (possibly) the */
- /* the currency symbol and intervening blank. */
-
- save_target = ptarget;
- if (yes_sym && (((pinfo->cfmt) & 1) == 0))
- {
- for (pcurr_sym = (pinfo->csym); *pcurr_sym; pcurr_sym++)
- *ptarget++ = *pcurr_sym;
- if ((pinfo->cfmt) & 2)
- *ptarget++ = ' ';
- }
-
- /* Next, the sign (if any). */
-
- if (sign)
- *ptarget++ = '-';
-
- /* Next, the digits preceding the decimal separator, along with */
- /* any thousands separators. */
-
- if (decptr > 0)
- while (decptr--)
- {
- *ptarget++ = *amtbuf++;
- if ((decptr > 0) && ((decptr % 3) == 0))
- if (thou_sep)
- *ptarget++ = thou_sep;
- }
- else
- *ptarget++ = '0';
-
- /* If the format calls for a decimal fraction, add the decimal */
- /* separator now. */
-
- if (precision)
- *ptarget++ = (pinfo->decsep)[0];
-
- /* Now copy the fractional part. */
-
- while (precision--)
- *ptarget++ = *amtbuf++;
-
- /* Finish with the intervening blank and currency symbol. */
-
- if (yes_sym && (((pinfo->cfmt) & 1) != 0))
- {
- if ((pinfo->cfmt) & 2)
- *ptarget++ = ' ';
- for (pcurr_sym = (pinfo->csym); *pcurr_sym; pcurr_sym++)
- *ptarget++ = *pcurr_sym;
- }
- *ptarget = '\0';
-
- return save_target;
- }