Mackenzie
Mackenzie

Reputation: 31

c++ How to put a double into a String

Well in java with a string you can do this:

int stuff;
string otherstuff;

otherstuff = "I like this much stuff: " + stuff;

But in C++ I have no idea how to.

Upvotes: 1

Views: 593

Answers (3)

odedsh
odedsh

Reputation: 2624

Also worth noting boost::lexical_cast<std::string>(stuff). If for some reason you can't use C++11

Upvotes: 1

Bogatyr
Bogatyr

Reputation: 19323

I like using stringstream for things like this.

std::stringstream ss;
double dub = 3.14159254;
ss << dub;
std::string s = ss.str();

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254431

In C++11:

otherstuff = "I like this much stuff: " + std::to_string(stuff);

Historically (and still sometimes useful in C++11):

std::ostringstream ss;
ss << "I like this much stuff: " << stuff;
otherstuff = ss.str();

Upvotes: 11

Related Questions