Home > Web Front-end > JS Tutorial > How to use javascript parseint method

How to use javascript parseint method

藏色散人
Release: 2021-10-18 11:24:34
Original
11245 people have browsed it

The function of the javascript parseint method is to parse a string and return an integer. The syntax is "parseInt(string, radix)", where the parameter string represents the string to be parsed.

How to use javascript parseint method

The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer

How to use the javascript parseint method?

JavaScript parseInt() function

Definition and usage

The parseInt() function parses a string and returns an integer.

When the value of parameter radix is ​​0, or the parameter is not set, parseInt() will determine the base of the number based on string.

When the parameter radix is ​​omitted, JavaScript defaults to the radix of numbers as follows:

If string starts with "0x", parseInt() will parse the rest of the string into hexadecimal integers.

If string begins with 0, then ECMAScript v3 allows an implementation of parseInt() to parse subsequent characters into octal or hexadecimal digits.

If string starts with a number from 1 to 9, parseInt() will parse it into a decimal integer.

Syntax

parseInt(string, radix)
Copy after login

Parameters

string Required. The string to be parsed.

radix Optional. Represents the base of the number to be parsed. The value is between 2 ~ 36.

The parseInt() function is supported by all major browsers

Tips and comments

Note: Only the first number in the string will be returned.

Note: Leading and trailing spaces are allowed.

Note: If the first character of the string cannot be converted to a number, parseInt() will return NaN.

Note: Older browsers default to octal base when strings start with "0". ECMAScript 5, the default is decimal base.

Example

We will use 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>");
Copy after login

The above example output results:

10
10
34
60
40
NaN

10
10
8
16
16
Copy after login

Note: Old browsers Since an old version of ECMAScript is used (the ECMAScript version is smaller than ECMAScript 5, octal is used by default when the string starts with "0", and ECMAScript 5 uses decimal), so parsing ("010") will output 8.

Recommended study: "javascript basic tutorial"

The above is the detailed content of How to use javascript parseint method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template