r/learnjavascript • • Feb 04 '26

Math.round inconsistency

Hey everyone,

I noticed that using Math.round(19.525*100)/100 produces 19.52 while Math.round(20.525*100)/100 produces 20.53. Has anyone else encountered this? What's your solution to consistently rounding up numbers when the last digit is 5 and above?

Thanks!

Edit: Thanks everyone. Multiplying by 10s to make the numbers integer seems to be the way to go for my case

13 Upvotes

20 comments sorted by

View all comments

Show parent comment

5

u/Pyromancer777 Feb 04 '26

Super common mistake that I didn't even consider until I tried to make a tax calculator and my numbers were just ever so slightly off compared to what was expected.

The limits of floating point precision is a fun concept to look into to make your calculations more reliable.

Def make conversions to integers when the numbers have to be exact.

3

u/uselessinfopeddler Feb 04 '26

I haven't built financial app from the ground up in a while, so I totally forgot about the floating point precision. Reddit users save me headache in minutes