_fcvt -- Convert Floating-Point to String

Format

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

Language Level: Extension
_fcvt converts the floating-point number value to a character string. _fcvt stores the digits of value as a string and adds a null character (\0). The ndec variable specifies the number of digits to be stored after the decimal point.

If the number of digits after the decimal point in value exceeds ndec, _fcvt rounds the correct digit according to the FORTRAN F format. If there are fewer than ndec digits of precision, _fcvt pads the string with zeros.

A FORTRAN F number has the following format:

_fcvt stores only digits 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 giving the position of the decimal point with respect to the beginning of the string. A 0 or negative integer value shows that the decimal point lies to the left of the first digit.

signptr points to an integer showing the sign of >value. _fcvt sets the integer to 0 if value is positive and to a nonzero number if value is negative.

_fcvt 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
_fcvt 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, _fcvt returns NULL and sets errno to ENOMEM.

Example
This example reads in two floating-point numbers, computes 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 given assumes the user enters the values 2000000 and 3000.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
   float x = 2000000;
   float y = 3000;
   double z;
   int w,b,decimal,sign;
   char *buffer;
   z = x *y;
   printf("The product of %e and %e is %g.\n", x, y, z);
   w = log10(fabs(z))+1.;
   buffer = _fcvt(z, w, &decimal, &sign);
   b = decimal-10;
   if (b < 0)
      printf("Their product does not exceed one billion.\n");
   if (b > 15)
      printf("The billions digit of their product is insignificant.\n");
   if ((b > -1) && (b < 16))
      printf("The billions digit of their product is %c.\n", buffer[b]);
   return 0;
   /*********************************************************************
      The output should be:
      The product of 2.000000e+06 and 3.000000e+03 is 6e+09.
      The billions digit of their product is 6.
   *********************************************************************/
}



_ecvt -- Convert Floating-Point to Character
_gcvt -- Convert Floating-Point to String
<stdlib>