_gcvt -- Convert Floating-Point to String

Format

#include <stdlib.h>
char *_gcvt(double value, int ndec, char *buffer);

Language Level: Extension
_gcvt converts a floating-point value to a character string pointed to by >buffer. The >buffer should be large enough to hold the converted value and a null character (\0) that _gcvt automatically adds to the end of the string. There is no provision for overflow.

_gcvt tries to produce ndec significant digits in FORTRAN F format. Failing that, it produces >ndec significant digits in FORTRAN E format. Trailing zeros might be suppressed in the conversion if they are insignificant.

A FORTRAN F number has the following format:

A FORTRAN E number has the following format:

On both OS/2 and Windows, _gcvt also converts NaN and infinity values to the strings NAN and INFINITY, respectively.

Note: For each thread, _ecvt, _fcvt and _gcvt 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
_gcvt returns a pointer to the string of digits. If it cannot allocate memory to perform the conversion, _gcvt returns an empty string and sets errno to ENOMEM.

Example
This example converts the value -3.1415e3 to a character string and places it in the character array buffer1. It then converts the macro value _INF to a character string and places it in buffer2.

#include <stdio.h>
#include <stdlib.h>
#if (1 == __TOS_OS2__)
#include <float.h>         /* for the definition of _INF */
#elseif
(1 == __TOS_WIN__)
#include <float.h>
#endif
int main(void)
{
   char buffer1[10],buffer2[10];
   _gcvt(-3.1415e3, 7, buffer1);
   printf("The first result is %s \n", buffer1);
#if (1 == __TOS_OS2__)
   _gcvt(_INF, 5, buffer2);
   printf("The second result is %s \n", buffer2);
#elseif
(1 == __TOS_WIN__)
   _gcvt(_INF, 5, buffer2);
   printf("The second result is %s \n", buffer2);
#endif
   return 0;
   /*****************************************************
      The output should be:
      The first result is -3141.5
      #if (1 == __TOS_OS2__)
         The second result is INFINITY
      #elseif (1 == __TOS_WIN__)
         The second result is INFINITY
      #endif
   *****************************************************/
}



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