Hey guys, simple doubt here. I was making the calculator project and I had this problem: 3.3 * 3 = 9.899999999999999.

Why it happens and how can I fix it?

Thanks, keep coding:)

Why 3.3 * 3 = 9.899999999999999?

0

Hey guys, simple doubt here. I was making the calculator project and I had this problem: 3.3 * 3 = 9.899999999999999.

Why it happens and how can I fix it?

Thanks, keep coding:)

I know it is a common thing on operations around coding. You’ll need to add a Math.round to the result. Math.round can take a second parameter defining how many decimals you want the number to be rounded to.

Thank you so much. The only thing that didn’t work was the second parameter. So I’ve solved with Math.round(**(3.3 * 3)** * 100) / 100.

This is a very common question actually

Floating point numbers cannot be represented exactly, which leads to common situations like yours in the first post.

There’s a great (short) pdf which I would highly recommend reading entitled “What every computer scientist should know about floating point arithmetic”

Edit: Hah okay it’s not that short, would still recommend reading it if looking for more depth, but take a look at this site instead: http://floating-point-gui.de/basic/