views:

14355

answers:

12

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.

A: 

Take a look at sprintf() and family.

Darron
And then shut your eyes and scream for mercy.
Daniel Earwicker
He said C++, not C. :)
jalf
But he did mention printf.
Darron
I mentioned printf because googling turned up a bunch of articles that say to convert from a double to a string you use printf. They make the assumption that the only thing you could be doing is printing, which in my case is false.
Bill the Lizard
+1  A: 

sprintf is your answer

Ed Marty
If the question is, "How do I create a buffer overflow?", then yes, sprintf() is the answer.
Fred Larson
@Fred Larson: Thank you for the *actual* LOL :)
e.James
There's always snprintf. There's still out-of-buffer issues, but you can create a decent sized buffer on the local stack for something like a single %lf.
Mr.Ree
+2  A: 

You could also use stringstream.

Firas Assaad
+20  A: 
// The C way:
char buffer[32];
snprintf(buffer, 32, "%g", myDoubleVar);

// The C++ way:
std::ostringstream sstream;
sstream << myDoubleVar;
std::string varAsString = sstream.str();

// The boost way:
std::string varAsString = boost::lexical_cast<std::string>(myDoubleVar);
Adam Rosenfield
No! Streaming to a temporary object is not a good idea. It will cause bad things when you try to stream something that isn't a primitive, like a char*.
coppro
Konrad Rudolph
It seems my stringstreams are a bit rusty, I prefer the ol' C ways. Fixed now (I hope).
Adam Rosenfield
Your C code threw me off at first. I still upvoted you for giving correct alternatives.
Bill the Lizard
+3  A: 

If you use C++, avoid sprintf. It's un-C++y and has several problems. Stringstreams are the method of choice, preferably encapsulated as in Boost.LexicalCast which can be done quite easily:

template <typename T>
std::string to_string(T const& value) {
    stringstream sstr;
    sstr << value;
    return sstr.str();
}

Usage:

string s = to_string(42.5);
Konrad Rudolph
+2  A: 

Heh, I just wrote this (unrelated to this question):

string temp = "";
stringstream outStream;
double ratio = (currentImage->width*1.0f)/currentImage->height;
outStream << " R: " << ratio;
temp = outStream.str();

/* rest of the code */
Vinko Vrsalovic
+3  A: 

sprintf is okay, but in C++, the better, safer, and also slightly slower way of doing the conversion is with stringstream:

#include <sstream>
#include <string>

// In some function:
double d = 453.23;
std::ostringstream os;
os << d;
std::string str = os.str();

You can also use Boost.LexicalCast:

#include <boost/lexical_cast.hpp>
#include <string>

// In some function:
double d = 453.23;
std::string str = boost::lexical_cast<string>(d);

In both instances, str should be "453.23" afterward. LexicalCast has some advantages in that it ensures the transformation is complete. It uses stringstreams internally.

coppro
+18  A: 

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();
Johannes Schaub - litb
Thanks. I'm using the Standard C++ way.
Bill the Lizard
I think boost::lexical_cast pretty much IS the standard C++ way, just nicely packaged for you. BTW, litb, you have a minor typo in there -- "boot:lexical_cast".
Fred Larson
+3  A: 

Herb Sutter has an excellent article on string formatting. I recommend reading it. I've linked it before on SO.

Fred Larson
+3  A: 

A question to answer your question: why are you storing the double value in a string to store it in a map? Are you going to use the string-ified double as the key? If not, why not leave the double as is?

Matt McClellan
Nice, I didn't even notice that part of the question.
Matt Cruikshank
Interesting point. Using a double as a map key may fraught with peril, however, as exact comparisons on floating point values always are. Indexing on a string representation avoids the problem.
Fred Larson
No, the double is going in as the value in a map<string, string>. I have a bunch of configuration parameters that are of mixed types, and I'm using the map as a convenient place to store them all.
Bill the Lizard
A: 

You may want to read my prior posting on SO. (Macro'ed version with a temporary ostringstream object.)

For the record: In my own code, I favor snprintf(). With a char array on the local stack, it's not that inefficient. (Well, maybe if you exceeded the array size and looped to do it twice...)

(I've also wrapped it via vsnprintf(). But that costs me some type checking. Yelp if you want the code...)

Mr.Ree
+1  A: 

The problem with lexical_cast is the inability to define precision. Normally if you are converting a double to a string, it is because you want to print it out. If the precision is too much or too little, it would affect your output.

Foxhunter