Home > Web Front-end > JS Tutorial > Definition and usage analysis of parseInt() function in javascript_javascript skills

Definition and usage analysis of parseInt() function in javascript_javascript skills

WBOY
Release: 2016-05-16 16:25:18
Original
1918 people have browsed it

The examples in this article describe the definition and usage of the parseInt() function in JavaScript. Share it with everyone for your reference. The specific analysis is as follows:

This function can parse a string and return an integer.

Grammar structure:

Copy code The code is as follows:
parseInt(string, type)

Parameter list:

Parameters Description
参数 描述
string 必需。要被解析的字符串。
type 可选。表示要解析的数字的基数,通俗的说就是数字的进制,比如二进制、八进制或者十六进制。该值介于2 ~ 36之间。
string Required. The string to be parsed.

type Optional. Indicates the base of the number to be parsed, which in layman's terms is the base of the number, such as binary, octal or hexadecimal. The value is between 2 ~ 36.
Detailed description:

1. Specify type parameter:


After specifying the type parameter, the function will parse the string according to the specified type parameter, for example:
1.parseInt("010",10), which means "010" is decimal, and the return value is 10.
2.parseInt("010",2), which means "010" is binary, and the return value is 2.
3.parseInt("010",8), which means "010" is octal, and the return value is 8.
4.parseInt("010",16), which means "010" is hexadecimal and the return value is 16.
Note: The return values ​​are all decimal. The type specifies the base of the first parameter, and the return value of the second parameter is between 2-36. If it is not in this range, the return value of the parseInt function is NaN. If the string parameters are not all numbers, but contain other characters, the parseInt function only returns the numbers before the first character. For example:

The return value of parseInt("123ab789",10) is 123, and everything after the first character a is omitted.

2. Do not specify the type parameter:

When the type parameter is not specified, the parseInt function will automatically determine which base it is, which is usually decimal, for example:


1.parseInt("23") The return value is 23.

2. The return value of parseInt("23ab") is 23.

But the situation is often not as simple as above. Let’s look at an example:


The return value of parseInt("0x12") is 18, which is not the number before returning the first string. There is a situation here. If the string starts with "0x", you should pay attention to it, because this At this time, the number after "0x" will be considered as hexadecimal, so the return value is 18. If it starts with "0" and is not followed by a character, then at this time, it will be parsed in decimal under Google Chrome, but it will be parsed in octal under IE browser. For example:

The return value of parseInt("0123") in Google Chrome is 123, and the return value in IE browser is 83.

I hope this article will be helpful to everyone’s JavaScript programming design.
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