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.
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?
Videos
// 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>
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.
You can use sprintf to do it, or maybe snprintf if you have it:
char str[ENOUGH];
sprintf(str, "%d", 42);
Where the number of characters (plus terminating char) in the str can be calculated using:
(int)((ceil(log10(num))+1)*sizeof(char))
As pointed out in a comment, itoa() is not a standard, so better use the sprintf() approach suggested in the rival answer!
You can use the itoa() function to convert your integer value to a string.
Here is an example:
int num = 321;
char snum[5];
// Convert 123 to string [buf]
itoa(num, snum, 10);
// Print our string
printf("%s\n", snum);
If you want to output your structure into a file there isn't any need to convert any value beforehand. You can just use the printf format specification to indicate how to output your values and use any of the operators from printf family to output your data.