I've read an article about
parseInt(0.5); // => 0 parseInt(0.05); // => 0 parseInt(0.005); // => 0 parseInt(0.0005); // => 0 parseInt(0.00005); // => 0 parseInt(0.000005); // => 0 parseInt(0.0000005); // => 5
Why is this happening?
I've searched about it on ecmascript standard, and saw this:
The first step Converting the input to string if it is not:
19.2.5 parseInt ( string, radix )
The parseInt function is the %parseInt% intrinsic object. When the parseInt function is called, the following steps are taken:
Let inputString be ? ToString(string).
- Let S be ! TrimString(inputString, start).
So, I've checked the values on string-based:
String(0.5); // => '0.5' String(0.05); // => '0.05' String(0.005); // => '0.005' String(0.0005); // => '0.0005' String(0.00005); // => '0.00005' String(0.000005); // => '0.000005' String(0.0000005); // => '5e-7'
So, it means when we use
parseInt(0.0000005), it is equal to
parseInt('5e-7') and based on the definition:
parseIntmay interpret only a leading portion of string as an integer value; it ignores any code units that cannot be interpreted as part of the notation of an integer, and no indication is given that any such code units were ignored.
So the answer will return
5 only because it is the only character which is a number till a noncharacter
e, so the rest of it
e-7 will be discarded.
(This is more of a long comment rather than a competing answer.)
The unwanted conversion leading to this weird effect only happens when passing the value to
parseInt as a number type - in this case it appears that the compiler (or interpreter or whatever drives JS) auto-converts the number into a string for you, as the string type is the expected type of the function parameter
That number-to-string conversion function, unfortunately, prefers the engineering number format when it would get too long otherwise.
Also, the conversion may lead to loss of precision, which is something every programmer has to be aware of whenever dealing with decimal (non-integer) numbers.
If you instead remember to place the to-be-parsed value into a string yourself, then you'll not get such unexpected results:
let n = '0.0000005'; console.log(parseInt(n))
Will print 0 as desired.
parseIntand similar functions that do not let you know if the string to be parsed has extra non-fitting data. E.g, int-parsing "x" or "1x" should both tell you that this is not a proper int number. In this regard,
parseIntexhibits bad behavior by ignoring extra data in the string and not telling us. A good parser function either tells you where it stopped (so that you can check if there's garbage left over) or fails when it finds unexpected data.
Please respect the data type!
In Chrome console: