Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Casting to std::string

Status
Not open for further replies.

moetman

Programmer
Nov 18, 2004
4
0
0
AU
Hi all,
I want to concatenate strings ans doubles into one string How do I cast a double to std::string ? Is there any way to use std::strings like osstringstream ? In the same way as os<<aDouble ?

Regards
 
You don't cast doubles to strings, you convert them using a stringstream and then copy the contents of the stringstream to the string:
Code:
double d = 3.3;
std::ostringstream os;
os << d;
std::string s = os.str();
 
You may also find it useful to use sprintf:

Code:
char buf[128];
double d = 3.3;
sprintf(buf, "%g", d);

string str = buf;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top