javascriptparseint

parseInt returning wrong values for values lower than 10^-7


parseInt(0.000001) returns 0

But

parseInt(0.0000001) returns 1


Does anyone happen to know the reason for this behaviour?

Solution

  • If the number is greater than 1e+21 (including) or less than 1e-7 (including), it will return 1. (when using radix/base 10).

    e.g.

    console.log(parseInt(0.0000001));
    console.log(parseInt(0.000000123));
    console.log(parseInt(1e-7));
    console.log(parseInt(1e+21));

    checkout mdn documantation:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#examples