I have just observed that the parseInt
function doesn't take care about the decimals in case of integers (numbers containing the e
character).
Let's take an example: -3.67394039744206e-15
> parseInt(-3.67394039744206e-15)
-3
> -3.67394039744206e-15.toFixed(19)
-3.6739e-15
> -3.67394039744206e-15.toFixed(2)
-0
> Math.round(-3.67394039744206e-15)
0
I expected that the parseInt
will also return 0
. What's going on at lower level? Why does parseInt
return 3
in this case (some snippets from the source code would be appreciated)?
In this example I'm using node v0.12.1
, but I expect same to happen in browser and other JavaScript engines.
I think the reason is parseInt
converts the passed value to string by calling ToString
which will return "-3.67394039744206e-15"
, then parses it so it will consider -3
and will return it.
The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN