JavaScript parseInt () function
Definition and Usage
parseInt () function to parse a string and returns an integer.
When the parameter radix is 0 or not set parameter, parseInt () based on the string to determine the number base.
When the parameter is ignored radix, JavaScript default base figure as follows:
- If the string beginning with "0x", parseInt () will resolve the rest of the string of hexadecimal integer.
- If the string 0 at the beginning, then the ECMAScript v3 allows parseInt () is an implementation of the subsequent character parsed as octal or hexadecimal numbers.
- If the string begins with a number from 1 to 9 to, parseInt () will resolve it to decimal integer.
grammar
参数 | 描述 |
---|---|
string | 必需。要被解析的字符串。 |
radix | 可选。表示要解析的数字的基数。该值介于 2 ~ 36 之间。 |
Browser Support
All major browsers support parseInt () function
Tips and Notes
Note: Only the first number in the string is returned.
Note: Leading and trailing spaces are allowed.
Note: If the first character can not be converted to digital, then parseFloat () returns NaN.
Note: The string "0" at the beginning of the old browser's default octal base. ECMAScript 5, the default decimal base.
Examples
Examples
We will use the parseInt () to parse different strings:
document.write(parseInt("10") + "<br>");
document.write(parseInt("10.33") + "<br>");
document.write(parseInt("34 45 66") + "<br>");
document.write(parseInt(" 60 ") + "<br>");
document.write(parseInt("40 years") + "<br>");
document.write(parseInt("He was 40") + "<br>");
document.write("<br>");
document.write(parseInt("10",10)+ "<br>");
document.write(parseInt("010")+ "<br>");
document.write(parseInt("10",8)+ "<br>");
document.write(parseInt("0x10")+ "<br>");
document.write(parseInt("10",16)+ "<br>");
</script>
Examples of the above output:
try it"
Note: old browsers due to the use of the old version of the ECMAScript (ECMAScript version less than ECMAScript 5, the default when a string of "0" at the beginning octal, ECMAScript 5 using a decimal), so parsing ( "010") will output 8 .
JavaScript Global Functions