Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the javascript parseint method

Shulou Source: shulou.com Published: 2022-06-03 04:46:44 10月06日 Update

This article mainly explains "how to use the javascript parseint method". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the javascript parseint method.

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

This article operating environment: windows7 system, javascript1.8.5 version, 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 the parameter radix is 0, or when it is not set, parseInt () determines the cardinality of the number based on string.

When the parameter radix is omitted, the cardinality of the default number for JavaScript is as follows:

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

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

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

Grammar

ParseInt (string, radix)

Parameters.

String is required. The string to be parsed.

Radix is optional. Represents the cardinality of the number to parse. The value is between 2 and 36.

All major browsers support the parseInt () function

Tips and comments

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

Note: spaces at the beginning and end are allowed.

Note: if the first character of the string cannot be converted to a number, parseInt () returns NaN.

Note: old browsers use octal cardinality by default when the string begins with "0". ECMAScript 5, the default is the decimal cardinality.

Example

We will use parseInt () to parse different strings:

[xss_clean] (parseInt ("10") + "

"); [xss_clean] (parseInt (" 10.33 ") +"

"); [xss_clean] (parseInt (" 34 45 66 ") +"

"); [xss_clean] (parseInt (" 60 ") +"

"); [xss_clean] (parseInt (" 40 years ") +"

"); [xss_clean] (parseInt (" He was 40 ") +"

); [xss_clean] ("

[xss_clean] (parseInt ("10", 10) + "

"); [xss_clean] (parseInt (" 010 ") +"

[xss_clean] (parseInt ("10", 8) + "

"); [xss_clean] (parseInt (" 0x10 ") +"

[xss_clean] (parseInt ("10", 16) + "

")

The output result of the above example:

1010346040NaN101081616

Note: because the old browser uses the old version of ECMAScript (the ECMAScript version is less than ECMAScript 5, octal is used by default when the string begins with "0", and ECMAScript 5 uses decimal), it will output 8 in parsing ("010").

Thank you for your reading, the above is the content of "how to use the javascript parseint method", after the study of this article, I believe you have a deeper understanding of how to use the javascript parseint method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Character string number method parameter cardinality beginning integer octal function decimal browser decimal learning browsing content hexadecimal instance version syntax Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple NVidia Docker Shulou Technology Huawei