In this article we will discuss three different ways to convert float to string in C++.
Table of Contents
Using ostringstream
to convert float to string in C++
In this method we use 0stringstream
to pass a float number to the buffer and then convert it into a string by using the str()
method. setprecision(int n)
function is also used with fixed to provide precision to floating numbers correct to decimal numbers passed as argument in the function. It is defined in the header.
Note: The string returned is only a temporary object and if other methods are called on the results, then it will yield a dangling pointer.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <iomanip>//for setprecision() #include <sstream> using namespace std; //Function to convert float to string string Convert (float number){ ostringstream buff; buff<<fixed<<setprecision(4)<<number;//setting the precision to four return buff.str();//converting to string } int main() { float n=9.2299595; string str1=Convert(n); //calling the function for conversion cout<<"After Converting to String: "<<str1; return 0; } |
Output
Using to_string()
function to convert float to string in C++
to_string()
function can also be used to convert float to string and it is defined in the header. This function can be used to convert different numeric values to string data type. It is very easy to implement and it can take floating point numbers as a parameter and will return string type value.
Note: This function can return unexpected output because the number of significant digits in the string returned by it can be zero as well and it can be seen in code.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> using namespace std; int main() { float f; cout<<"Enter the float number to convert to string: "; cin>>f; string float_string(to_string(f)); if(float_string.empty()){ cout<<"Empty Number!!"; } cout<<"After Converting to String: "<<float_string << endl; return 0; } |
Output
After Converting to String: 58.258282
Using Macro expression to convert float to string in C++
This method will work only for literal floating point numbers. In this method, preprocessor macros can be utilized to convert constant floating point number to string. In the coding part, we can see the right way to call the string variable constructor. We need #operator
for STRING macro to convert float to string in C++.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> using namespace std; #define STRING(number) #number int main() { string float_str(STRING(9876.5412)); if(float_str.empty()){ cout << "EMPTY\n"; } else{ cout <<"After converting to string: "<< float_str << endl; } return 0; } |
Output
If we define a constant floating point number as another macro
expression, then we can use two-level macro to get the correct results.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> using namespace std; #define STRING(number) STR(number) #define STR(number) #number #define NUMBER 9876.5412 int main() { string float_str(STRING(NUMBER)); if(float_str.empty()){ cout << "EMPTY\n"; } else{ cout <<"After converting to string: "<< float_str << endl; } return 0; } |
Output
Using lexical_cast method from the Boost libraries to convert float to String in C++
Boost library has a lexical_cast function which can be used to convert float to String.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <boost/lexical_cast.hpp> using namespace std; using boost::lexical_cast; int main() { float f; cout<<"Enter the float number to convert to string: "; cin>>f; string float_string = lexical_cast<string>(f); if(float_string.empty()){ cout<<"Empty Number!!"; } cout<<"After Converting to String: "<<float_string << endl; return 0; } |
Output
After Converting to String: 23.228
Conclusion
In this article, we discussed three different ways to convert float to string in C++. In all the methods, we have to take care of a few things while using them.
Happy Learning!!