Definition and usage
The parseInt () function parses a string and returns an integer.
Syntax
ParseInt (string, radix)
Parameter description
String is required. The string to be parsed.
Radix
Optional. The base number of The number to be parsed. The value ranges from 2 ~ Between 36.
If this parameter is omitted or its value is 0, the number is parsed based on 10. If it starts with "0x" or "0X", it will base on 16.
If this parameter is smaller than 2 or greater than 36, parseInt () returns NaN.
Return Value
Returns the parsed number.
Description
When the value of the radix parameter is 0, or this parameter is not set, parseInt () determines the base number based on the string.
For example, if the string starts with "0x", parseInt () resolves the rest of the string to a hexadecimal integer. If the string starts with 0, ECMAScript v3 allows an implementation of parseInt () to parse the subsequent characters into octal or hexadecimal numbers. If string is 1 ~ Start with a number of 9, parseInt () will resolve it to a decimal integer.
Tips and comments
Note: Only the first digit in the string is returned.
Note: spaces at the beginning and end are allowed.
Tip: if the first character of a string cannot be converted to a number, parseFloat () returns NaN.
Instance
In this example, we will use parseInt () to parse different strings:
ParseInt ("10"); // returns 10
ParseInt ("19", 10); // return 19 (10 + 9)
ParseInt ("11", 2); // returns 3 (2 + 1)
ParseInt ("17", 8); // returns 15 (8 + 7)
ParseInt ("1f", 16); // 31 (16 + 15) is returned)
ParseInt ("010"); // Undetermined: return 10 or 8
ParseInt ("010", 10); // return 10
ParseInt ("0011", 10); // return