Precise double casting in C

Parsing precise double values to strings can be a bit tricky in C. Usually in C it looks like the following:

double doubleValue = 0.9999996;
char stringAsDouble[32];
snprintf(stringAsDouble, 32, "%lf", doubleValue);

In C++ the cast looks a bit different:

double doubleValue = 0.9999996;
std::ostringstream doubleAsStringStream;
doubleAsStringStream << doubleValue;
std::string doubleAsString = doubleAsStringStream.str();

One more way is to use boost libraries:

double doubleValue = 0.9999996;
std::string doubleAsString = boost::lexical_cast(doubleValue);

The problem is that at least the C and C++ option will round the value after six digits giving you "1.0" instead of "0.9999996" although doubles should habe a precision of at least 14 decimal places. I was able to have a more precise cast by using the following piece of code were I explicitley defined the precision by "%.14f". This is the maximum presicion of double (at least on my system).

double doubleValue = 0.9999996;
char doubleAsString[sizeof(double)*8];
std::snprintf(doubleAsString,sizeof doubleAsString,"%.14f",doubleValue);