0

I had a float varible that was 130.975006 and I printed it out as %.2f, I were expacting to see 130.97 and for some reason I got 130.98 and only when I switched it to a double varible I got the 130.97

why does, when I want to print out a floating point number with only two numbers after the decimal point it get's rounded up, but the same thing doesn't happen to a double variable, and is there a way to bypass it? for exmple why does the output of this code: `

#include <stdio.h>

int main(){
  float num1 = 130.975000;
  double num2 = 130.975000;
  printf("float is %.2f,double is %.2lf",num1,num2);
  return 0;
}

`

is: "float is 130.98,double is 130.97"

and how can I print out a float varible with only two numbers after the decimal point without rounding the number?

Ofri K
  • 3
  • 3
  • Perhaps a single-precision `float` variable just don't have the precision enough to store the exact value, while a double-precision `double` have? Generally, there's really no need for `float` any more, just use `double` everywhere. – Some programmer dude Nov 15 '22 at 09:43
  • 1
    Also note that for `printf`, there's no difference between `%f` and `%lf`, because `float` values are *promoted* to `double` when used in vararg functions like `printf`. This promotion will not be able to add precision that isn't in the actual value though, so once you've lost precision by storing in a `float` variable, you can't get that back. – Some programmer dude Nov 15 '22 at 09:46
  • 2
    When you try `printf("float is %.25f,double is %.25f",num1,num2);` you will see that the `float` is `130.975006`, so gets rounded up and the `double` is `130.974999999999`, so gets rounded down. https://godbolt.org/z/KPEav779z – mch Nov 15 '22 at 09:59
  • Maybe related: https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – chtz Nov 15 '22 at 10:55
  • Ofri K, Try `printf("float is %a, double is %a\n",num1,num2);` to see the complete values of both. – chux - Reinstate Monica Nov 15 '22 at 11:00
  • 2
    "how can I print out a float varible with only two numbers after the decimal point without rounding the number?" --> To avoid rounding during the `printf()` with 2 digits after the decimal point, only print values that end with `xxx.00, xxx.25, xxx.50, xxx.75`, else the result will be _rounded_. – chux - Reinstate Monica Nov 15 '22 at 11:06
  • First off, `printf` always rounds floating-point numbers, because you just about always want it to. There's no way to turn the rounding behavior off. Secondly, floating-point numbers are *not* stored internally using decimal digits. They're *binary*, and it turns out that the number 130.975 can not be represented exactly in binary. As single-precision `float`, the closest you can get is about 130.975006, and as double-precision `double`, the closest you can get is about 130.97499999999999. – Steve Summit Nov 16 '22 at 21:09
  • Finally, that's why you got different answers: 130.975006 rounds up to 130.98, while 130.97499999999999 rounds down to 130.97. – Steve Summit Nov 16 '22 at 21:10

0 Answers0