Format
#include <stdlib.h>
char *_gcvt(double value, int ndec, char *buffer);
Language Level: Extension
Threadsafe: Yes.
Locale Sensitive: The behavior of this function might be affected by the LC_CTYPE and LC_NUMERIC categories of the current locale. For more information, see Understanding CCSIDs and Locales.
Description
_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() attempts 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:
>>-+----+--digit--+-------+--.--+-------+---------------------->< +-+--+ '-digit-' '-digit-' '- --'
A FORTRAN E number has the following format:
>>-+----+--digit--.--digit--E--+----+--digit--+-------+-------->< +-+--+ +-+--+ '-digit-' '- --' '- --'
_gcvt also converts infinity values to the string INFINITY.
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 that uses _gcvt()
This example converts the value -3.1415e3 to a character string and places it in the character array buffer1.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buffer1[10];
_gcvt(-3.1415e3, 7, buffer1);
printf("The first result is %s \n", buffer1);
return 0;
}
The output should be:
The first result is -3141.5
Related Information: