. Advertisement .
..3..
. Advertisement .
..4..
The following tutorial guides you how to convert float to string in C++ with detailed examples.
How To Convert Float To String In C++
Method 1: Use stringstream
There is a class stringstream in C++, which offers stream-like functionality. This function allows you to convert float to string:
Input:
#include <iostream>
#include <string>
#include<sstream>
using namespace std;
int main()
{
float number = 1024.671734;
stringstream stream;
// Set precision level to 3
stream.precision(3);
stream << fixed;
// Convert float to string
stream<<number;
string str = stream.str();
cout<<str<<endl;
return 0;
}
Output:
1024.672
Method 2: Use boost::lexical_cast()
This template function is used for data type conversion. It converts your chosen elements from one type to another, including a float and string.
Input:
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
int main()
{
float number = 1024.671734;
// Convert float to string using boost library
string str = lexical_cast<string>(number);
cout<<str<<endl;
return 0;
}
Output:
1024.67175
Method 3: Use to_string()
This method keeps the original precision level:
Input:
#include <iostream>
#include <string>
using namespace std;
int main()
{
float number = 1024.671734;
// Convert float to string
string num_str = to_string(number);
cout<<num_str<<endl;
return 0;
}
Output:
1024.671753
Method 4: Use sprintf()
The function separates the fractional and integral parts while converting them to strings. Here is an example:
Input:
// 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"
Conclusion
In this tutorial, we have discussed four methods to convert float to string in C++. There are detailed examples so we hope that you can use them effectively.
Leave a comment