fcvt() in C/C++ with Examples
This function fcvt() converts the floating-point value to NULL-terminated ASCII string and returns a pointer to it. It is defined in the library function defined in stdlib.h header file.
char * fcvt (double value, int num, int * dec, int * sign);
1. double value: It is the floating-point value that will convert into a string.
2. int num: It is a number of digits to be returned by the function. If this is greater than the number of digits in value then the rest of the string is padded with zeros and if it is smaller the low-order digit is rounded.
3. int * dec: It is an integer pointer, which stores the decimal point position with respect to beginning the string. If it is zero or less than zero, indicates that the decimal point lies to the left of the digits
4. int * sign: It is an integer pointer, which receives the sign indicator like 0 means positive sign and non-zero means negative.
The function returns a character string terminated with null with the same length specified as num that contains the digits of the double number passes as a parameter.
Below are the C programs to illustrate the use of fcvt() function:
x = 123.4567;
buf = fcvt(x, 6, &dec, &sign);
"The converted string "
"value is: %c%c.%sX10^%d\n"
sign == 0 ?
, buf, dec);
The converted string value is: +0.123456700X10^3
In the above C program, the double(float) value i.e., 123.4567 is converted into the string value(+ 0.123457X103) using fcvt() function.
ndig = 10;
value = -9.876;
string = fcvt(value, ndig,
"The converted string"
" value is: %s Dec "
"is: %d sign is: %d\n"
string, Dec, sign);
The converted string value is: 98760000000 Dec is: 1 sign is: 1