Numbers not adding exactly right

Hello, I have a question about basic addition in javaScript. When adding numbers with decimals together, the answer sometimes comes out different from what I expect. I’m not sure what’s going on. If anyone can help me understand, that would be appreciated.

3.97 + 5.4 \returns 9.370000000000001

I expected 9.37


2.97 + 7.48 \returns 10.450000000000001


3.97 + 5.47 + 3.62 \returns 13.059999999999999

How do I make sure I get what I’m expecting rather than getting these slightly off returns?



This is a known problem in digital systems (loosely because of binary vs. base 10 arithmetic).

Depending on what exactly you need out of your output, you can look into functions that round to a specific decimal, or floor/ceiling functions.

I would suggest browsing the MDN docs with some of those specific keywords.

For more background:

1 Like

That makes perfect sense. Thanks so much for that!