SoFunction
Updated on 2025-03-01

About a weird behavior solution for parseInt in JavaScript

parseInt()It's a built-inJavaScriptfunction, which parses a string of numerics into integers. For example, we will use a numeric string'100'Resolve to integer:

const number = parseInt('100');
number; // 100

As expected,'100'Resolved as integers100

parseInt(numericalString, radix)Also accept the second parameter:radix(cardinality), another parameter is a numeric stringnumericalStringradixParameters allow you to parse strings from different digit bases as integers, and the bases usually have2, 8, 10 and 16

We use the cardinality as2ofparseInt()To parse a numeric string:

const number = parseInt('100', 2);
number; // 4

parseInt('100', 2)By base2Analysis'100'for an integer: so it returns a decimal value4

That's rightparseInt()A brief introduction to the method.

1. A weird behavior in parseInt()

parseInt(numericalString)Always convert its first argument to a string (if it is not a string), and then parse this string number into an integer.

That's why you can (But this should not be done!)useparseInt()To extract the integer part of a floating point number:

parseInt(0.5); // => 0
parseInt(0.05); // => 0
parseInt(0.005); // => 0
parseInt(0.0005); // => 0
parseInt(0.00005); // => 0
parseInt(0.000005); // => 0

Take out floating point numbers, for example0.50.05etc. The result is0. This is the same as our expected results.

Then extract0.0000005What happens to the integer part?

parseInt(0.0000005); // => 5

parseInt()Add floating point numbers0.0000005Resolved as5. This is interesting and not as expected...

WhyparseInt(0.0000005)Will there be such a weird behavior?

2. Solve parseInt() the weird behavior

Let's review,parseInt(numericalString)What is done with its first argument: if it is not a string, convert it to a string, parse, and then return the parsed integer.

This may be the first clue.

We manually convert floating point numbers to string representations:

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'

String(0.0000005)The explicit conversion of a numeric value is different from other floating point numbers: it is an exponential symbolexponential notationString representation.

This is the second clue -- very important!

Then when the exponent symbol string parses into an integer, you will get the integer5

parseInt(0.0000005); // => 5
// same as
parseInt(5e-7); // => 5
// same as
parseInt('5e-7'); // => 5

parseInt('5e-7')Consider the first number'5', skipped'e-7'

Decrypted! becauseparseInt()Always convert its first parameter to a string, and floating-point numbers less than 10−610^{-6}10−6 will be written as exponential symbols.parseInt()Take the integer from the exponential symbol of the floating point number.

Translator added -- note,parseInt()Take the part of the integer before the string, for example:parseInt('12Jimmy34')There will be a return result12

Note: Safely take out the part of floating point integers, I recommend using()function:

(0.5); // => 0
(0.05); // => 0
(0.005); // => 0
(0.0005); // => 0
(0.00005); // => 0
(0.000005); // => 0
(0.0000005); // => 0

3. Summary

parseInt()is a function that converts a string of numbers into integers.

When you useparseInt()Be careful when getting the integer part of a floating point number.

Floats less than 10−610^{-6}10−6 (e.g.0.0000005Equivalent to 5∗10−75 * 10^{-7}5∗10−7) is converted to be represented in exponential symbols (e.g.5e-7yes0.0000005exponential symbolic form). This is whyparseInt()Unexpected effects occur when acting on such a small floating point number: only important parts of the exponential representation (e.g.5e-7In-house5)。

Challenge: You can explain whyparseInt(999999999999999999999)Will be equal to1

This is the article about solving a weird behavior of parseInt() in JavaScript. For more related content of parseInt() in js, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!

Original linkSolving a Mystery Behavior of parseInt() in JavaScript-- authorDmitri Pavlutin