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
Share:
87,386
kosmoplan
Author by

kosmoplan

Updated on July 12, 2022

Comments

  • kosmoplan
    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 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.

  • paulsm4
    paulsm4 over 11 years
    Absolutely 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
    Pascal Cuoq over 11 years
    Technically neither float nor double can store the value 0.0000003455, although they both can store something close enough.
  • Admin
    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.