home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / MATHSRC.ZIP / EFCVT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.1 KB  |  123 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - efcvt.c
  3.  *
  4.  * function(s)
  5.  *        ecvt - converts a floating-point number to a string
  6.  *        fcvt - converts a floating-point number to a string
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #include <stdlib.h>
  19. #include <_printf.h>
  20. #include <RtlData.h>
  21.  
  22. #if !defined( _RTLDLL )
  23. static  char    _cvtBuf [__XCVTDIG__ + 2];
  24. #endif
  25.  
  26.  
  27. /*--------------------------------------------------------------------------*
  28.  
  29. Name            ecvt - converts a floating-point number to a string
  30.  
  31. Usage           char *ecvt(double value, int ndigit, int *decpt, int *sign);
  32.                 char *fcvt(double value, int ndigit, int *decpt, int *sign);
  33.  
  34. Prototype in    stdlib.h
  35.  
  36. Description     ecvt converts  value to a null-terminated  string of ndigit
  37.                 digits and returns a pointer to the string. The position of
  38.                 the decimal point  relative to the beginning of  the string
  39.                 is stored  indirectly through decpt  (a negative value  for
  40.                 decpt means  to the left   of the returned  digits). If the
  41.                 sign of the result is negative, the word pointed to by sign
  42.                 is  non-zero; otherwise,  it is  0. The  low-order digit is
  43.                 rounded.
  44.  
  45.                 fcvt is identical to ecvt except that the correct digit has
  46.                 been rounded for  FORTRAN F-format output of the  number of
  47.                 digits specified by ndigit.
  48.  
  49. Return value    The return  values of ecvt   and fcvt point  to static data
  50.                 whose content is overwritten by each call to ecvt or fcvt.
  51.  
  52. *---------------------------------------------------------------------------*/
  53. char  _FAR * ecvt  (double value, int nDig, int _FAR *decP, int _FAR *signP )
  54.  
  55. /*
  56. The double (value) is converted to a decimal string (_cvtBuf) of up to 18
  57. digits, a sign (*signP, false == positive) and a decimal exponent (*decP).
  58.  
  59. "nDig" specifies the number of significant digits.
  60.   If nDig < 1  then nDig = 1.
  61.   If nDig > 18 then nDig = 18.
  62.  
  63. The string is in ASCIIZ form.  The string is padded with zeros to the
  64. right to fill in the requested number of digits or decimal places.
  65.  
  66. The exponent *decP is calculated as if the decimal point were at the left
  67. (most significant) end of the string (there is no "." character in the
  68. string).
  69.  
  70. If the value was zero then the exponent is 0 and the string is all "0".  If
  71. the value was infinite or NAN then the exponent is 999 and the string is
  72. all "9".
  73.  
  74. The conversion to a numeral string is done in __xcvt, with only some
  75. parameter checking and result storage needs to be done here.
  76.  
  77. The return value is a pointer to _cvtBuf.
  78. */
  79.  
  80. {
  81.     *decP = __xcvt (& value,
  82.                    (nDig > 1) ? nDig : 1,
  83.                    signP,
  84.                    _RTLInstanceData(_cvtBuf),
  85.                    F_8byteFloat
  86.                    );
  87.     return _RTLInstanceData(_cvtBuf);
  88. }
  89.  
  90. /*---------------------------------------------------------------------*
  91.  
  92. Name            fcvt - converts a floating-point number to a string
  93.  
  94. Usage           char *fcvt(double value, int ndigit, int *decpt, int *sign);
  95.  
  96. Prototype in    stdlib.h
  97.  
  98. Description     see ecvt
  99.  
  100. *---------------------------------------------------------------------*/
  101. char  _FAR * fcvt (double value, int nDig, int _FAR *decP, int _FAR *signP )
  102.  
  103. /*
  104. Identical to ecvt except for the interpretation of nDig.
  105.  
  106. "nDig" specifies the number of significant digits following the
  107.   decimal point.
  108.   If nDig < 1  then nDig = 1.
  109.   If nDig > 18 then nDig = 18.
  110.  
  111. Also, the total of digits both left and right of the decimal point shall
  112. not exceed 18.
  113. */
  114.  
  115. {
  116.     *decP = __xcvt ( &value,
  117.                      (nDig > 0) ? -nDig : 0,
  118.                      signP,
  119.                      _RTLInstanceData(_cvtBuf),
  120.                      F_8byteFloat);
  121.     return _RTLInstanceData(_cvtBuf);
  122. }
  123.