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