Parse non-decimal numbers
Convert binary, hexadecimal, or another-base text into a JavaScript integer.
JS Puzzles · Const
parseInt(str, radix) reads an integer prefix using the specified base, from 2 through 36. It stops at the first character that is not valid in that base and returns the digits read so far.
parseInt() converts its first argument to a string, reads an optional sign and leading whitespace, then consumes as many valid digits as possible in the chosen radix. It returns the resulting integer, or NaN if no valid digit can be read.
parseInt(str, radix)| Name | Type | Description |
|---|---|---|
str | string or value convertible to string | The value to convert; it is interpreted as a string. |
radix | number | The numeral-system base used for parsing, from 2 through 36. |
An integer parsed from the string; the function returns NaN when no valid number can be read.
const decimal = parseInt("101", 2);
const hexadecimal = parseInt("ff", 16);
const partial = parseInt("42px", 10);
console.log(decimal); // 5
console.log(hexadecimal); // 255
console.log(partial); // 42The radix explicitly selects the numeral system, while parsing stops when a character is not valid in that radix.
Convert binary, hexadecimal, or another-base text into a JavaScript integer.
Extract an integer prefix from text such as "42px".
Convert user or file input into an integer after choosing the intended numeral system.
parseFloatNumber.toString