_ecvt -- Convert Floating-Point to Character

Format

#include <stdlib.h>
char *_ecvt(double value, int ndigits, int *decptr, int *signptr);

Language Level: Extension
_ecvt converts the floating-point number value to a character string. _ecvt stores ndigits digits of value as a string and adds a null character (\0). If the number of digits in value exceeds ndigits, the low-order digit is rounded. If there are fewer than ndigits digits, the string is padded with zeros. Only digits are stored in the string.

You can obtain the position of the decimal point and the sign of value after the call from decptr and signptr. decptr points to an integer value that gives the position of the decimal point with respect to the beginning of the string. A 0 or a negative integer value indicates that the decimal point lies to the left of the first digit.

signptr points to an integer that indicates the sign of the converted number. If the integer value is 0, the number is positive. If it is not 0, the number is negative.

_ecvt also converts NaN and infinity values to the strings NAN and INFINITY, respectively.

Warning!
For each thread, the _ecvt, _fcvt and _gcvt functions use a single, dynamically allocated buffer for the conversion. Any subsequent call that the same thread makes to these functions destroys the result of the previous call.

Return Value
_ecvt returns a pointer to the string of digits. Because of the limited precision of the double type, no more than 16 decimal digits are significant in any conversion. If it cannot allocate memory to perform the conversion. _ecvt returns NULL and sets errno to ENOMEM.

Example
This example reads in two floating-point numbers, calculates their product, and prints out only the billions digit of its character representation. At most, 16 decimal digits of significance can be expected. The output assumes the user enters the numbers 1000000 and 3000.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
   float x,y;
   double z;
   int w,b,decimal,sign;
   char *buffer;
   printf("Enter two floating-point numbers:\n");
   if (2 != scanf("%e %e", &x, &y)) {
      printf("input error...\n");
      return EXIT_FAILURE;
   }
   z = x *y;
   printf("Their product is %g\n", z);
   w = log10(fabs(z))+1.;
   buffer = _ecvt(z, w, &decimal, &sign);
   b = decimal-10;
   if (b < 0)
      printf("Their product does not exceed one billion.\n");
   else
      if (b > 15)
         printf("The billions digit of their product is insignificant.\n");
      else
         printf("The billions digit of their product is %c.\n", buffer[b]);
   return 0;
   /************************************************************************
      For the following input:
      1000000 3000
      The output should be:
      Enter two floating-point numbers:
      1000000 3000
      Their product is 3e+09
      The billions digit of their product is 3.
   ************************************************************************/
}



_fcvt -- Convert Floating-Point to String
_gcvt -- Convert Floating-Point to String
<stdlib.h>