-2

Possible Duplicate:
Convert double to string C++?

however, thanks everyone, I have a float number:1.000, how to convert it to a std::string ,like "1.000".

Community
  • 1
  • 1
yetuweiba
  • 241
  • 1
  • 4
  • 13

2 Answers2

1
std::stringstream stream;
float f = 1.000f;
stream << f;
std::string str;
stream >> str;
Nikola Smiljanić
  • 26,745
  • 6
  • 48
  • 60
1

You can either use stringstream:

float val = 1.000f;
stringstream ss;
ss << val;
string stringVal = ss.str();

or use boost::lexical_cast<>() (which also uses stringstream underneath)

#include <boost/lexical_cast.hpp>
float val = 1.000f;
string stringVal = boost::lexical_cast<string>( val );
Bart
  • 19,692
  • 7
  • 68
  • 77
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • HeHe,thanks,but it is not work in my computer,I have tried sstream and boost.It is useless.It output 1.at last,thanks for help me. – yetuweiba Dec 09 '11 at 12:25