How do I convert a double into a string in C++?

I need to store a double as a string. I know I can use printf if I wanted to display it, but I just want to store it in a string variable so that I can store it in a map later (as the value, not the key).

Answers


The boost (tm) way:

std::string str = boost::lexical_cast<std::string>(dbl);

The Standard C++ way:

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();

Note: Don't forget #include <sstream>


Need Your Help

Simple Ragel Example that Balances Parentheses?

ruby ragel

Here is a starting point for a grammar:

Shared javax.ejb.Singleton accross all wars

java-ee ejb java-ee-6 cdi

I'm using WebSphere 8.5.5 JEE6 Appserver.