How to convert float to string in C++?
In C++, you can convert a float type to a string type using the std::to_string() function, as shown below:
#include <iostream>
#include <string>
int main() {
float number = 3.14f;
std::string str = std::to_string(number);
std::cout << "Float number: " << number << std::endl;
std::cout << "String representation: " << str << std::endl;
return 0;
}
When running the program, the output is:
Float number: 3.14
String representation: 3.140000