Definition | #include <stdlib.h> char *fcvt(double value, int n, int *dec_pt, int *sign);
The string begins with the first non-zero digit of the floating-point number to be converted and includes n decimal places. The decimal point and a negative sign, if any, do not form a part of the string. However, |
Parameters | double value Floating-point value that is to be edited for output. int n Number of digits after the decimal point. If n is less than the number of digits after the decimal point in value, the least significant digit is rounded (as in the FORTRAN F format). int *dec_pt Pointer to an integer that specifies the position of the decimal point in the result string. Positive number: position relative to the beginning of the result string. int *sign Pointer to an integer that specifies the sign of the result string. 0: the sign is positive |
Return val. | Pointer to the converted string. |
Notes | Invalid parameters, e.g. an Note that the arguments dec_pt and sign must be pointers!
|
Example | The following program reads a floating-point value x, converts it as specified in n according to the FORTRAN F format, and outputs it as a string. In addition, the calculated sign and the position of the decimal point dec_pt are output. #include <stdio.h> #include <stdlib.h> int main(void) { double x; int n, dec_pt, sign; printf("Please enter floating-point number: \n"); if (scanf("%lf", &x) == 1) { printf("How many significant digits?: \n"); if (scanf("%d", &n) == 1) { printf("The converted number is : %s \n", fcvt(x, n, &dec_pt, &sign)); printf("The sign is %s \n", (sign == 0 ? "positive" : "negative")); printf("The position of the decimal point is: %d \n", dec_pt); } } return 0; } |
See also | ecvt, gcvt |