Convert a floating point number to string in C Last Updated : 21 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: ftoa(1.555, str, 2) should store "1.55" in res.ftoa(1.555, str, 0) should store "1" in res. 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. Extract integer part from floating-point or double number.First, convert integer part to the string.Extract fraction part by exacted integer part from n.If d is non-zero, then do the following.Convert fraction part to an integer by multiplying it with pow(10, d)Convert the integer value to string and append to the result. Following is C implementation of the above approach. C // C program for implementation of ftoa() #include <math.h> #include <stdio.h> // Reverses a string 'str' of length 'len' void reverse(char* str, int len) { int i = 0, j = len - 1, temp; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } } // Converts a given integer x to string str[]. // d is the number of digits required in the output. // If d is more than the number of digits in x, // then 0s are added at the beginning. int intToStr(int x, char str[], int d) { int i = 0; while (x) { str[i++] = (x % 10) + '0'; x = x / 10; } // If number of digits required is more, then // add 0s at the beginning while (i < d) str[i++] = '0'; reverse(str, i); str[i] = '\0'; return i; } // Converts a floating-point/double number to a string. void ftoa(float n, char* res, int afterpoint) { // Extract integer part int ipart = (int)n; // Extract floating part float fpart = n - (float)ipart; // convert integer part to string int i = intToStr(ipart, res, 0); // check for display option after point if (afterpoint != 0) { res[i] = '.'; // add dot // Get the value of fraction part upto given no. // of points after dot. The third parameter // is needed to handle cases like 233.007 fpart = fpart * pow(10, afterpoint); intToStr((int)fpart, res + i + 1, afterpoint); } } // Driver program to test above function int main() { char res[20]; float n = 233.007; ftoa(n, res, 4); printf("\"%s\"\n", res); return 0; } Output:"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. Comment More infoAdvertise with us Next Article Convert a floating point number to string in C K kartik Improve Article Tags : C++ cpp-data-types cpp-string Practice Tags : CPP Similar Reads Converting Number to String in C++ In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let's look at som 4 min read Rounding Floating Point Number To two Decimal Places in C and C++ How to round off a floating point value to two places. For example, 5.567 should become 5.57 and 5.534 should become 5.53 First Method:- Using Float precision C++ #include<bits/stdc++.h> using namespace std; int main() { float var = 37.66666; // Directly print the number with .2f precision cou 2 min read Convert String to int in C++ In C++, both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. There are 6 significant methods to convert strings to numbers in C++ as follows:1. String to int Conversion Using st 6 min read C++ Program to Convert String to Integer Given a string of digits, the task is to convert the string to an integer. Examples: Input : str = "12345" Output : 12345 Input : str = "876538"; Output : 876538 Input : str = "0028"; Output : 28 CPP // C++ program to convert String into Integer #include <bits/stdc++.h> using namespace std; // 1 min read Comparison of a float with a value in C Predict the output of the following C program. C #include<stdio.h> int main() { float x = 0.1; if (x == 0.1) printf("IF"); else if (x == 0.1f) printf("ELSE IF"); else printf("ELSE"); } The output of above program is "ELSE IF" which means the expression "x == 0.1" 4 min read Like