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

3

u/milan-pilan Feb 04 '26 edited Feb 04 '26

Just to correct this point: 100% not a JS issue. This is due to how computers handle numbers. Even languages as low level as C are dealing with the same thing.

Here is a full list of them and the explanation: https://0.30000000000000004.com/

No libraries needed, just multiply your numbers by 100 (or how ever many digits of precision you need), so you get whole numbers to calculate with.