Convert Float to String In C++
In this article, we learn how we can convert float to string in C++ using different methods:
- Using the to_string()
- Using stringstream
- Using Macros
- Using lexical_cast from the boost library
1. Using to_string()
The to_string() method takes a single integer variable or other data type and converts it into a string.
Syntax: –
string to_string (float value);
Example:
C++
#include <bits/stdc++.h> using namespace std; int main() { float x=5.5; string resultant; resultant=to_string(x); cout << "Converted value from float to String using to_string() is : " <<resultant<<endl; return 0; } |
Converted value from float to String using to_string() is : 5.500000
Explanation: The to_string function converts the given float value into the string.
2. Using stringstream
A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input. The basic methods are:
- clear() – To clear the stream.
- str() – To get and set a string object whose content is present in the stream.
- operator <<- Add a string to the stringstream object.
- operator >>- Read something from the stringstream object.
Example:
C++
#include <bits/stdc++.h> using namespace std; int main() { float x=5.5; stringstream s; s<<x; // appending the float value to the streamclass string result=s.str(); //converting the float value to string cout << "Converted value from float to String using stringstream is : " <<result<<endl; return 0; } |
Converted value from float to String using stringstream is : 5.5
Explanation: The stringstream class converts the float value from a variable to a string. It is an inbuilt class present in the C++ Library.
3. Using Macros
This method solely applies to floating point conversion. The ‘#’ is used by the STRING macro to convert the floating-point values to String.
Syntax:
#define STRING(Value) #Value string gfg(STRING(Float_value));
Example:
C++
#include <bits/stdc++.h> #include <string> using namespace std; //using macro to convert float to string #define STRING(Value) #Value int main() { string gfg(STRING(5.5)); if (gfg.empty()) cout << "The String is empty" <<endl ; else cout << gfg << endl; return EXIT_SUCCESS; } |
5.5
4. Using lexical_cast
Boost.LexicalCast which is defined in the Library “boost/lexical_cast.hpp” provides a cast operator, boost::lexical_cast, that can convert numbers from strings to numeric types like int or double and vice versa. boost::lexical_cast is an alternative to functions like std::stoi(), std::stod(), and std::to_string(), which were added to the standard library in C++11.
Syntax:
float x= 5.5; string res = lexical_cast<string>(x);
Example:
C++
#include "boost/lexical_cast.hpp" #include <bits/stdc++.h> using namespace std; using boost::lexical_cast; using boost::bad_lexical_cast; int main() { // Float to string conversion float x= 5.5; string res = lexical_cast<string>(x); cout << res << endl; return 0; } |
Output:
5.5
Please Login to comment...