Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of decimal digits in C

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 in float.h? If so, how could I change that from 6 to 10?

I'm getting a number like 0.000000 while the actual value is 0.0000003455.

like image 646
kosmoplan Avatar asked Oct 06 '12 16:10

kosmoplan


1 Answers

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
like image 132
epsalon Avatar answered Sep 26 '22 06:09

epsalon