Previous: Parsing of Numbers, Up: Arithmetic

The old System V C library provided three functions to convert numbers to strings, with unusual and hard-to-use semantics. The GNU C library also provides these functions and some natural extensions.

These functions are only available in glibc and on systems descended
from AT&T Unix. Therefore, unless these functions do precisely what you
need, it is better to use `sprintf`

, which is standard.

All these functions are defined in `stdlib.h`.

— Function: char * **ecvt** (`double value, int ndigit, int *decpt, int *neg`)

The function

`ecvt`

converts the floating-point numbervalueto a string with at mostndigitdecimal digits. The returned string contains no decimal point or sign. The first digit of the string is non-zero (unlessvalueis actually zero) and the last digit is rounded to nearest.`*`

decptis set to the index in the string of the first digit after the decimal point.`*`

negis set to a nonzero value ifvalueis negative, zero otherwise.If

ndigitdecimal digits would exceed the precision of a`double`

it is reduced to a system-specific value.The returned string is statically allocated and overwritten by each call to

`ecvt`

.If

valueis zero, it is implementation defined whether`*`

decptis`0`

or`1`

.For example:

`ecvt (12.3, 5, &d, &n)`

returns`"12300"`

and setsdto`2`

andnto`0`

.

— Function: char * **fcvt** (`double value, int ndigit, int *decpt, int *neg`)

The function

`fcvt`

is like`ecvt`

, butndigitspecifies the number of digits after the decimal point. Ifndigitis less than zero,valueis rounded to thendigit+1'th place to the left of the decimal point. For example, ifndigitis`-1`

,valuewill be rounded to the nearest 10. Ifndigitis negative and larger than the number of digits to the left of the decimal point invalue,valuewill be rounded to one significant digit.If

ndigitdecimal digits would exceed the precision of a`double`

it is reduced to a system-specific value.The returned string is statically allocated and overwritten by each call to

`fcvt`

.

— Function: char * **gcvt** (`double value, int ndigit, char *buf`)

`gcvt`

is functionally equivalent to `sprintf(buf, "%*g", ndigit, value'. It is provided only for compatibility's sake. It returnsbuf.If

ndigitdecimal digits would exceed the precision of a`double`

it is reduced to a system-specific value.

As extensions, the GNU C library provides versions of these three
functions that take `long double`

arguments.

— Function: char * **qecvt** (`long double value, int ndigit, int *decpt, int *neg`)

This function is equivalent to

`ecvt`

except that it takes a`long double`

for the first parameter and thatndigitis restricted by the precision of a`long double`

.

— Function: char * **qfcvt** (`long double value, int ndigit, int *decpt, int *neg`)

This function is equivalent to

`fcvt`

except that it takes a`long double`

for the first parameter and thatndigitis restricted by the precision of a`long double`

.

— Function: char * **qgcvt** (`long double value, int ndigit, char *buf`)

This function is equivalent to

`gcvt`

except that it takes a`long double`

for the first parameter and thatndigitis restricted by the precision of a`long double`

.

The `ecvt`

and `fcvt`

functions, and their `long double`

equivalents, all return a string located in a static buffer which is
overwritten by the next call to the function. The GNU C library
provides another set of extended functions which write the converted
string into a user-supplied buffer. These have the conventional
`_r`

suffix.

`gcvt_r`

is not necessary, because `gcvt`

already uses a
user-supplied buffer.

— Function: char * **ecvt_r** (`double value, int ndigit, int *decpt, int *neg, char *buf, size_t len`)

The

`ecvt_r`

function is the same as`ecvt`

, except that it places its result into the user-specified buffer pointed to bybuf, with lengthlen.This function is a GNU extension.

— Function: char * **fcvt_r** (`double value, int ndigit, int *decpt, int *neg, char *buf, size_t len`)

The

`fcvt_r`

function is the same as`fcvt`

, except that it places its result into the user-specified buffer pointed to bybuf, with lengthlen.This function is a GNU extension.