0

I am doing the following calculation with javascript: 0.1111111111111115 * Math.pow(10,18)

it gave me: 111111111111111490

however, if I do (0.1111111111111115 * Math.pow(10,17))*10 it will give me the correct result: 111111111111111500

why will it happen and how to prevent it?

Thank you!

  • There is only a certain amount of precision (number of significant digits) in the double floating point system that Javascript uses. If you overflow or underflow that, even in an intermediate result, you get wrong results. This is a well known issue that there are thousands of articles on and is not unique to Javascript. For integer math, Javascript now has `BigInt` which is limitless, but uses a different type of number and only works on integers. – jfriend00 Aug 21 '22 at 17:01
  • I tried the decimal library to resolve the problem. Thank you everyone – CHANG DING Aug 21 '22 at 17:54

0 Answers0