The short answer to print floating point numbers losslessly (such that they can be read back in to exactly the same number, except NaN and Infinity):
printf("%.9g", number)
.printf("%.17g", number)
.Do NOT use %f
, since that only specifies how many significant digits after the decimal and will truncate small numbers. For reference, the magic numbers 9 and 17 can be found in float.h
which defines FLT_DECIMAL_DIG
and DBL_DECIMAL_DIG
.