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

  1. /***
  2. *gcvt.c - convert floating point number to character string
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Converts floating point number into character string representation.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <fltintrn.h>
  13. #include <internal.h>
  14. #include <nlsint.h>
  15.  
  16. /***
  17. *double _gcvt(value, ndec, buffer) - convert floating point value to char
  18. *       string
  19. *
  20. *Purpose:
  21. *       _gcvt converts the value to a null terminated ASCII string
  22. *       buf.  It attempts to produce ndigit significant digits
  23. *       in Fortran F format if possible, ortherwise E format,
  24. *       ready for printing.  Trailing zeros may be suppressed.
  25. *       No error checking or overflow protection is provided.
  26. *       NOTE - to avoid the possibility of generating floating
  27. *       point instructions in this code we fool the compiler
  28. *       about the type of the 'value' parameter using a struct.
  29. *       This is OK since all we do is pass it off as a
  30. *       parameter.
  31. *
  32. *Entry:
  33. *       value - double - number to be converted
  34. *       ndec - int - number of significant digits
  35. *       buf - char * - buffer to place result
  36. *
  37. *Exit:
  38. *       result is written into buffer; it will be overwritten if it has
  39. *       not been made big enough.
  40. *
  41. *Exceptions:
  42. *
  43. *******************************************************************************/
  44.  
  45. char * __cdecl _gcvt (
  46.         double value,
  47.         int ndec,
  48.         char *buf
  49.         )
  50. {
  51.  
  52. #ifdef _MT
  53.         struct _strflt strfltstruct;    /* temporary buffers */
  54.         char   resultstring[21];
  55. #endif  /* _MT */
  56.  
  57.         STRFLT string;
  58.         int    magnitude;
  59.         char   *rc;
  60.  
  61.         REG1 char *str;
  62.         REG2 char *stop;
  63.  
  64.         /* get the magnitude of the number */
  65.  
  66. #ifdef _MT
  67.         string = _fltout2( value, &strfltstruct, resultstring );
  68. #else  /* _MT */
  69.         string = _fltout( value );
  70. #endif  /* _MT */
  71.  
  72.         magnitude = string->decpt - 1;
  73.  
  74.         /* output the result according to the Fortran G format as outlined in
  75.            Fortran language specification */
  76.  
  77.         if ( magnitude < -1  ||  magnitude > ndec-1 )
  78.                 /* then  Ew.d  d = ndec */
  79.                 rc = str = _cftoe( &value, buf, ndec-1, 0);
  80.         else
  81.                 /* Fw.d  where d = ndec-string->decpt */
  82.                 rc = str = _cftof( &value, buf, ndec-string->decpt );
  83.  
  84.         while (*str && *str != *__decimal_point)
  85.                 str++;
  86.  
  87.         if (*str++) {
  88.                 while (*str && *str != 'e')
  89.                         str++;
  90.  
  91.                 stop = str--;
  92.  
  93.                 while (*str == '0')
  94.                         str--;
  95.  
  96.                 while (*++str = *stop++)
  97.                         ;
  98.         }
  99.  
  100.         return(rc);
  101. }
  102.