home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / _fptostr.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  94 lines

  1. /***
  2. *_fptostr.c - workhorse routine for converting floating point to string
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Workhorse routine for fcvt, ecvt.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <string.h>
  13. #include <fltintrn.h>
  14.  
  15. /***
  16. *void _fptostr(buf, digits, pflt) - workhorse floating point conversion
  17. *
  18. *Purpose:
  19. *       This is the workhorse routine for fcvt, ecvt. Here is where
  20. *       all the digits are put into a buffer and the rounding is
  21. *       performed and indicators of the decimal point position are set. Note,
  22. *       this must not change the mantissa field of pflt since routines which
  23. *       use this routine rely on this being unchanged.
  24. *
  25. *Entry:
  26. *       char *buf - the buffer in which the digits are to be put
  27. *       int digits - the number of digits which are to go into the buffer
  28. *       STRFLT pflt - a pointer to a structure containing information on the
  29. *               floating point value, including a string containing the
  30. *               non-zero significant digits of the mantissa.
  31. *
  32. *Exit:
  33. *       Changes the contents of the buffer and also may increment the decpt
  34. *       field of the structure pointer to by the 'pflt' parameter if overflow
  35. *       occurs during rounding (e.g. 9.999999... gets rounded to 10.000...).
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. void __cdecl _fptostr (
  42.         char *buf,
  43.         REG4 int digits,
  44.         REG3 STRFLT pflt
  45.         )
  46. {
  47.         REG1 char *pbuf = buf;
  48.         REG2 char *mantissa = pflt->mantissa;
  49.  
  50.         /* initialize the first digit in the buffer to '0' (NOTE - NOT '\0')
  51.          * and set the pointer to the second digit of the buffer.  The first
  52.          * digit is used to handle overflow on rounding (e.g. 9.9999...
  53.          * becomes 10.000...) which requires a carry into the first digit.
  54.          */
  55.  
  56.         *pbuf++ = '0';
  57.  
  58.         /* Copy the digits of the value into the buffer (with 0 padding)
  59.          * and insert the terminating null character.
  60.          */
  61.  
  62.         while (digits > 0) {
  63.                 *pbuf++ = (*mantissa) ? *mantissa++ : (char)'0';
  64.                 digits--;
  65.         }
  66.         *pbuf = '\0';
  67.  
  68.         /* do any rounding which may be needed.  Note - if digits < 0 don't
  69.          * do any rounding since in this case, the rounding occurs in  a digit
  70.          * which will not be output beause of the precision requested
  71.          */
  72.  
  73.         if (digits >= 0 && *mantissa >= '5') {
  74.                 pbuf--;
  75.                 while (*pbuf == '9')
  76.                         *pbuf-- = '0';
  77.                 *pbuf += 1;
  78.         }
  79.  
  80.         if (*buf == '1') {
  81.                 /* the rounding caused overflow into the leading digit (e.g.
  82.                  * 9.999.. went to 10.000...), so increment the decpt position
  83.                  * by 1
  84.                  */
  85.                 pflt->decpt++;
  86.         }
  87.         else {
  88.                 /* move the entire string to the left one digit to remove the
  89.                  * unused overflow digit.
  90.                  */
  91.                 memmove(buf, buf+1, strlen(buf+1)+1);
  92.         }
  93. }
  94.