Definition | #include <stdlib.h> char *ecvt(double value, int n, int *dec_pt, int *sign);
The string begins with the first non-zero digit of the floating-point number, i.e. leading zeros are not included. 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 in the result string (calculated from the first non-zero digit of the floating-point number to be converted). If n is less than the number of digits in value, the least significant digit is rounded. int *dec_pt Pointer to an integer specifying 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 specifying the sign of the result string. 0: the sign is positive |
Return val. | Pointer to the converted string.
|
Notes | An invalid parameter, such as 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, 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; char *s; printf("Please enter floating-point number: \n"); if (scanf("%lf", &x) == 1) { printf("How many significant digits?: \n"); if (scanf("%d", &n) == 1) { s = ecvt(x, n, &dec_pt, &sign); printf("The string is: %s\n", s); 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 | fcvt, gcvt |