0

Why is:

Math.round(6.0/11.0*10000.0)*0.01  // = 54.550000000000004

but

Math.round(6.0/11.0*10000.0)/100.0 // = 54.55

And what would be the right way to round this correctly.
For example I have int x, int y and I need a rounded x/y*100 on 2 decimal points.
Thank you.

Master M
  • 221
  • 2
  • 6
  • 2
    http://floating-point-gui.de/ –  Mar 28 '19 at 13:55
  • 1
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Reimeus Mar 28 '19 at 13:56
  • 54.55 can't be represented exactly in binary so both calculations will be slightly inexact. If you want to **display** the number rounded you should ask about that. You'll never get the `double` itself to be exactly 54.55. – John Kugelman Mar 28 '19 at 14:01

0 Answers0