Float can store this number of decimal only if the number is small, otherwise use double.
In this example %.17g
and %.14f
are working without problem :
#include <stdio.h>
int main(void)
{
double v = 0.12345678912345;
printf("%.17g %.14f \n", v, v);
return 0;
}
Displayed result :
0.12345678912345 0.12345678912345
From the documentation
f : Decimal floating point, lowercase 392.65
e : Scientific notation (mantissa/exponent), lowercase 3.9265e+2
g : Use the shortest representation: %e or %f 392.65
So using %.14f
it is fine
Edit:
the small values (less than 1.0e-20) are not properly converted to string.
To display more than 20 decimal, you should use long double... But if you only need to store 1.0e-20 and do not need to print more than 6 decimal, float can hold it.
For long double, you need to use something like %.21Lg
. For example :
#include <stdio.h>
int main(void)
{
long double v = 0.123456789123456789123456789;
printf("%.21Lg %.21Lf \n", v, v);
return 0;
}