Number of decimal digits in C
87,386
There are two separate issues here: The precision of the floating point number stored, which is determined by using float
vs double
and then there's the precision of the number being printed as such:
float foo = 0.0123456789;
printf("%.4f\n", foo); // This will print 0.0123 (4 digits).
double bar = 0.012345678912345;
printf("%.10lf\n", bar); // This will print 0.0123456789
Author by
kosmoplan
Updated on July 12, 2022Comments
-
kosmoplan almost 2 years
I like to change the number of decimal digits showed whenever I use a float number in C. Does it have something to do with the
FLT_DIG
value defined infloat.h
? If so, how could I change that from 6 to 10?I'm getting a number like
0.000000
while the actual value is0.0000003455
. -
paulsm4 over 11 yearsAbsolutely correct. Both "float" (and "double") can store the value "0.0000003455", but you need to specify "%.10xx" in order to print it. Great response :)!
-
Pascal Cuoq over 11 yearsTechnically neither
float
nordouble
can store the value 0.0000003455, although they both can store something close enough. -
Admin over 11 years@paulsm4 You really need to take a crash course about floating-point representation instead of trolling around on SO and downvoting here and there. +1 for Pascal Cuoq.