
#include <iostream> #include <sstream> #include <iomanip>
std::string float2string(float value) { std::ostringstream streamObj; // Set Fixed -Point Notation streamObj << std::fixed; // Set precision to 2 digits streamObj << std::setprecision(2); //Add double to stream streamObj << value; // Get string from output string stream return streamObj.str(); }
int main() { float value = 3.14159; std::string valueAsString = float2string(value); std::cout << valueAsString << std::endl; // Prints "3.14" return 0; }