0

I´ve come across this oddity when writing some JS code. It happens in both Chrome and Firefox:

10.04 + 0.01 = 10.049999999999999

Chrome

Firefox

Not sure if I´m just missing something obvious that causes this?

Right now I am handling it with rounding the number afterwards - Is there a better way to avoid this bug/feature?

thomasf1
  • 1,782
  • 4
  • 18
  • 32

1 Answers1

0

This is a consequence of the way numbers are stored internally in IEEE 754 form.

To avoid this, use a BigDecimal library such as this one: https://www.npmjs.com/package/js-big-decimal

Andrew Parks
  • 6,358
  • 2
  • 12
  • 27