type conversion - Convert float to std::string in C++ -
I have float value that should be inserted into std :: string
. How do I convert from float to string?
float val = 2.5; Std :: string my_val = val; // error here
Unless you are concerned about performance, its Use:
std :: ostringstream ss; SS & lt; & Lt; MyFloat; Std :: string s (ss.str ());
If you are OK with Boost, then there is a convenient option:
std :: string s = boost :: lexical_cast & lt; Std :: string & gt; (MyFloat);
Efficient options eg Or just the C-style function.
Comments
Post a Comment