![]() |
VOOZH | about |
Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string.
ftoa(n, res, afterpoint) n --> Input Number res[] --> Array where output string to be stored afterpoint --> Number of digits to be considered after the point.
Example:
We strongly recommend to minimize the browser and try this yourself first. A simple way is to use sprintf(), but use of standard library functions for direct conversion is not allowed. Approach: The idea is to separate integral and fractional parts and convert them to strings separately. Following are the detailed steps.
Following is C implementation of the above approach.
"233.0070"
Time Complexity: O(logn)
Auxiliary Space: O(1)
Note: The program performs similar operation if instead of float, a double type is taken.