gcvt() — Convert double to string

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <stdlib.h>

char *gcvt(double x, int ndigit, char *buf);

General description

The gcvt() function converts double floating-point argument values to floating-point output strings. The gcvt() function has been extended to determine the floating-point format (hexadecimal floating-point or IEEE Binary Floating-Point) of double argument values by using __isBFP().

z/OS® XL C/C++ formatted output functions, including the gcvt() function, convert IEEE Binary Floating-Point infinity and NaN argument values to special infinity and NaN floating-point number output sequences. See “fprintf Family of Formatted Output Functions” on fprintf(), printf(), sprintf() — Format and write data for a description of the special infinity and NaN output sequences.

The gcvt() function converts x to a NULL-terminated string (similar to the %g format of fprintf(), printf(), sprintf() — Format and write data) in the array pointed to by buf and returns buf. It produces ndigit significant digits (limited to an unspecified value determined by the precision of a double) in %f if possible, or %e (scientific notation) otherwise. A minus sign is included in the returned string if value is less than 0. A radix character is included in the returned string if value is not a whole number. Trailing zeros are suppressed where value is not a whole number. The radix character is determined by the current locale. If setlocale() — Set locale has not been called successfully, the default locale, “POSIX”, is used. The default locale specifies a period (.) as the radix character. The LC_NUMERIC category determines the value of the radix character within the current locale.

Note: This function has been moved to the Legacy Option group in Single UNIX Specification, Version 3 and may be withdrawn in a future version. The sprintf() function is preferred for portability.

Returned value

If successful, gcvt() returns the character equivalent of x as specified above.

If unsuccessful, gcvt() returns NULL.

Related information