I have the following two parseInt() and I am not quite sure why they gave me different results:
alert(parseInt(0.00001))
shows 0;
alert(parseInt(0.00000001))
shows 1
My guess is that since parseInt needs string parameter, it treats 0.00001
as ""+0.00001
which is "0.00001"
, therefore, the first alert will show 0
after parseInt. For the second statement, ""+0.00000001
will be "1e-8"
, whose parseInt will be 1
. Am I correct?
Thanks
parseInt(0.00001, 10)
– Quadratic0.00001
, but then you said it shows0
. It shows0
for me. – AphelionString(0.00000001)
--> "1e-8" andparseInt("1e-8");
-> 1 – ExplicitparseInt
(a string parsing function) as a means to round a number. This shows exactly why you shouldn't do it! – Burgenland