0

I want to multiply the result of Math.Pow by a double (2 decimals). Here is an example:

const number = 2.01*Math.pow(10,9);

Expected result: 2010000000

Actual Result: 2009999999.9999998

What causes this slight deviation? Is there a solution to this problem without rounding the incorrect number?

cqx
  • 275
  • 4
  • 11
  • Unfortunately, it is a common problem in many languages: https://0.30000000000000004.com/ A javascript solution could be: https://github.com/MikeMcl/decimal.js/ – Alessio Koci Mar 14 '20 at 20:01
  • 1
    Javascript is not so good with calculation of big numbers, or saving many digits after zero. but for your particular case you can just write `2.01e9` which will do exactly what you are trying to do – Yosef Tukachinsky Apr 17 '20 at 15:03
  • Also you can try to seperate the integer part from the decimal part, like so: `2*Math.pow(10,9)+.01*Math.pow(10,9)` – Yosef Tukachinsky Apr 17 '20 at 15:11

0 Answers0