Dear friends,

The division 0.03%0.01 should be 0, however we get 0.009999999999999998 . Do you know any workaround?

Thanks!

Dear friends,

The division 0.03%0.01 should be 0, however we get 0.009999999999999998 . Do you know any workaround?

Thanks!

Itâ€™s a floating point rounding error. Floating point is an efficient and flexible way to store arbitrarily large or small numbers, but unfortunately it comes at the cost of some accuracy. Itâ€™s also, infuriatingly, the only numerical type that JavaScript natively supports.

For many purposes (such as currency), multiplying by 10 or 100 and then dividing by that again when outputting the answer will give you the precision you need.

Thanks for your answer. I know that, but I thought that probably there is an elegant solution of this â€¦

Well, it depends what your definition of â€śelegantâ€ť is. If you donâ€™t mind using a library, https://github.com/MikeMcl/bignumber.js/ usually comes highly recommended.

If youâ€™re interested in the theory, hereâ€™s some further reading: https://medium.com/dailyjs/javascripts-number-type-8d59199db1b6

1 Like

you can use `toFixed`

or `toPrecision`

to get around the rounding.

Thatâ€™s a good idea in many cases, but wonâ€™t work in the OPâ€™s case:

```
(0.3 % 0.1).toFixed(1); //evaluates to "0.1", but should be "0.0"
```

@lionel-rowe

Thanks! The first link is useful â€¦