Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the javascript parseint method

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report