Use snprintf() from stdlib.h. Worked for me.
double num = 123412341234.123456789;
char output[50];
snprintf(output, 50, "%f", num);
printf("%s", output);
Answer from sujeeth.mr on Stack OverflowUse snprintf() from stdlib.h. Worked for me.
double num = 123412341234.123456789;
char output[50];
snprintf(output, 50, "%f", num);
printf("%s", output);
The only exact solution is to perform arbitrary-precision decimal arithmetic for the base conversion, since the exact value can be very long - for 80-bit long double, up to about 10000 decimal places. Fortunately it's "only" up to about 700 places or so for IEEE double.
Rather than working with individual decimal digits, it's helpful to instead work base-1-billion (the highest power of 10 that fits in a 32-bit integer) and then convert these "base-1-billion digits" to 9 decimal digits each at the end of your computation.
I have a very dense (rather hard to read) but efficient implementation here, under LGPL MIT license:
http://git.musl-libc.org/cgit/musl/blob/src/stdio/vfprintf.c?h=v1.1.6
If you strip out all the hex float support, infinity/nan support, %g/%f/%e variation support, rounding (which will never be needed if you only want exact answers), and other things you might not need, the remaining code is rather simple.
Double to string and string to double
Convert double to string C++? - Stack Overflow
converting string to a double variable in C - Stack Overflow
Faster way to convert double to string, not using "%f"?
// The C way:
char buffer[32];
snprintf(buffer, sizeof(buffer), "%g", myDoubleVar);
// The C++03 way:
std::ostringstream sstream;
sstream << myDoubleVar;
std::string varAsString = sstream.str();
// The C++11 way:
std::string varAsString = std::to_string(myDoubleVar);
// The boost way:
std::string varAsString = boost::lexical_cast<std::string>(myDoubleVar);
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>
I'd like a pair of functions atof and ftoa. The atof function converts a string (char *) into a double. The ftoa function converts a double into a string (char *). The first and most obvious requirement is that atof(ftoa(x)) = x for all double-precision floating point values x including special values (ยฑ0, ยฑโ and nan). My second requirement is that ftoa generate the shortest possible representation for all inputs (including denormalized floats) such that the first condition is satisfied, e.g. it can choose between %f and %g for this purpose.
Does such a pair of functions exist and, if so, where can I download the C source code to them?
You can't do it directly. There are a number of ways to do it:
Use a
std::stringstream:std::ostringstream s; s << "(" << c1 << ", " << c2 << ")"; storedCorrect[count] = s.str()Use
boost::lexical_cast:storedCorrect[count] = "(" + boost::lexical_cast<std::string>(c1) + ", " + boost::lexical_cast<std::string>(c2) + ")";Use
std::snprintf:char buffer[256]; // make sure this is big enough!!! snprintf(buffer, sizeof(buffer), "(%g, %g)", c1, c2); storedCorrect[count] = buffer;
There are a number of other ways, using various double-to-string conversion functions, but these are the main ways you'll see it done.
In C++11, use std::to_string if you can accept the default format (%f).
storedCorrect[count]= "(" + std::to_string(c1) + ", " + std::to_string(c2) + ")";
Have to output millions of doubles to text files, so wondering if there is a faster way to convert double => text, besides using %f in one of many settings printf, sprintf, others I could probably find.
In the way that you could convert an int to text by using modulo and division to get the decimal digits. I assume that's faster than running through %d.
EDIT: I finally settled on %d and multiplying the double values by 1000000, converting to int, then converting back on the receiving end. More than enough precision for my specific application, not really elegant, but the time saving is significant. The double->int and reverse conversions are just a few instructions, much faster than converting the double to a string and back to double.