6

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

AlliceSmash
  • 687
  • 1
  • 11
  • 19
  • Always add a radix, `parseInt(0.00001, 10)` – adeneo Apr 19 '14 at 20:52
  • Your question is confusing. You first said that the first alert shows `0.00001`, but then you said it shows `0`. It shows `0` for me. – Barmar Apr 19 '14 at 20:54
  • Seems like you're on the right track: `String(0.00000001)` --> "1e-8" and `parseInt("1e-8");` -> 1 – Andrew Apr 19 '14 at 20:56
  • Barmar: you are right, the first alert shows 0. Sorry about that – AlliceSmash Apr 19 '14 at 20:57
  • Over and over I see people incorrectly using `parseInt` (a _string parsing function_) as a means to round a number. This shows exactly why you shouldn't do it! – Alnitak Apr 19 '14 at 20:59
  • 1
    @Alnitak:I am not trying to use parseInt to round a number. I was reading JQuery Types documentation and noticed parseInt( 0.000001) returns 1 and I did not understand. That is why I tried to use different values to figure out why. But thanks for pointing this out so others will be aware. – AlliceSmash Apr 19 '14 at 21:18

3 Answers3

8

I believe you are correct.

parseInt(0.00001) == parseInt(String(0.00001)) == parseInt('0.00001') ==> 0

parseInt(0.00000001) == parseInt(String(0.00000001)) == parseInt('1e-8') ==> 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

parseInt takes each character in the first argument (converted to a string) that it recognizes as a number, and as soon as it finds a non-numeric value it ignores that value and the rest of the string. (see MDN second paragraph under "Description")

Therefore it's likely that parseInt(0.00000001) === parseInt(String(0.00000001)) === parseInt("1e-8"), which would only extract the 1 from the string yielding parseInt("1") === 1

However, there's another possibility:

From Mozilla developer network: parseInt(string, radix);

for the string argument (emphasis added): "The value to parse. If string is not a string, then it is converted to one. Leading whitespace in the string is ignored."

I think this possibility is less likely, since String(0.00000001) does not yield NAN.

Andrew
  • 946
  • 9
  • 19
0

You are correct.

parseInt is intended to get a number from a string. So, if you pass it a number, it first converts it into a string, and then back into a number. After string conversion, parseInt starts at the first number in the string and gives up at the first non-number related character. So "1.e-8" becomes "1"

If you know you are starting with a string, and are just trying to get an Integer value, you can do something like.

Math.round(Number('0.00000001')); // 0

If you know you have a floating point number and not a string...

Math.round(0.00000001); // 0

You can also truncate, ceil(), or floor the number

mrbinky3000
  • 4,055
  • 8
  • 43
  • 54