const number = parseInt('100');number; // 100
'100' is parsed to integer
parseInt(numericalString, radix) also accepts a second argument: the radix at which the numerical string argument is. The radix argument allows you to parse integers from different
numerical bases, the most common being 2, 8, 10, and 16.
parseInt() to parse a numerical string in base 2:
const number = parseInt('100', 2);number; // 4
parseInt('100', 2) parses
'100' as an integer in numerical base 2: thus it returns the value
4 (in decimal).
That's pretty much a short introduction to
Before I go on, let me recommend something to you.
parseInt(numericalString) always converts its first argument to a string (if it's not a string), then parses that numeric string to the integer value.
That's why you can (but should't!) use
parseInt() to extract the integer part of float numbers:
parseInt(0.5); // => 0parseInt(0.05); // => 0parseInt(0.005); // => 0parseInt(0.0005); // => 0parseInt(0.00005); // => 0parseInt(0.000005); // => 0
Extracting the integer part of floats like
0.05, etc. results in
0. This works as expected.
What about extracting the integer part of
parseInt(0.0000005); // => 5
parseInt() parses the float
5. Interesting and kind of unexpected...
parseInt(0.0000005) have such a mystery behavior?
Let's look again at what
parseInt(numericalString) does with its first argument: if it's not a string, then it is converted to a string, then parsed, and the parsed integer returned.
That might be the first clue.
Let's try then to convert manually the floats to a string represenation:
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'
The explicit conversion to a string of
String(0.0000005) behaves differently than other floats: it's a string representation of the exponential notation!
That's the second — and a significant clue!
And when the expontential notiation is parsed to an integer, you get the number
parseInt(0.0000005); // => 5// same asparseInt(5e-7); // => 5// same asparseInt('5e-7'); // => 5
parseInt('5e-7') takes into consideration the first digit
'5', but skips
Mystery solved! Because
parseInt() always converts its first argument to a string, the floats smaller than 10-6 are written in an exponential notation. Then
parseInt() extracts the integer from the exponential notation of the float.
On a side note, to safely extract the integer part of a float number I recommend
Math.floor(0.5); // => 0Math.floor(0.05); // => 0Math.floor(0.005); // => 0Math.floor(0.0005); // => 0Math.floor(0.00005); // => 0Math.floor(0.000005); // => 0Math.floor(0.0000005); // => 0
parseInt() is the function that parses numerical strings to integers.
Care must be taken when trying to extract the integer part of floats using
Floats smaller than 10-6 (e.g.
0.0000005 which is same as 5*10-7) conversed to a string are written in the exponential notation (e.g.
5e-7 is the exponential notation of
0.0000005). That's why using such small floats with
parseInt() leads to unexpected results: only the significat part (e.g.
5e-7) of the exponential notiation is parsed.
Side challenge: can you explain why
1? Write your considerations in a comment below!