This article mainly introduces the definition and usage of the parseInt () function in javascript, analyzes in detail the definition and usage of the parseInt () function, and precautions for using parameters, you can refer to the examples in this article to describe the definition and usage of parseInt () function in javascript. Share it with you for your reference. The specific analysis is as follows:
This function can parse a string and return an integer.
Syntax structure:
The Code is as follows:
ParseInt (string, type)
Parameter List:
Parameters |
Description |
String |
Required. The string to be parsed. |
Type |
Optional. Indicates the base number of The number to be parsed. Generally speaking, it is the hexadecimal number, such as binary, octal, or hexadecimal. The value ranges from 2 ~ Between 36. |
Detailed description:
1. specify the type parameter:
After the type parameter is specified, the function parses the string according to the specified type parameter. For example:
1. parseInt ("010", 10) indicates that "010" is in decimal format, and the return value is 10.
2. parseInt ("010", 2) indicates that "010" is binary, And the return value is 2.
3. parseInt ("010", 8) indicates that "010" is octal, And the return value is 8.
4. parseInt ("010", 16) indicates that "010" is hexadecimal and the return value is 16.
Description: The return value is in decimal format. type indicates that the first parameter is in hexadecimal format, and the return value of the second parameter is between 2 and 36. If the return value is not in this range, the parseInt function returns NaN. If the string parameter is not a number but contains other characters, the parseInt function returns only the number before the first character. For example:
The return value of parseInt ("123ab789", 10) is 123, and all values after the first character a are omitted.
2. do not specify the type parameter:
When the type parameter is not specified, the parseInt function automatically determines the base system, which is usually in decimal format. For example:
1. parseInt ("23") returns 23.
2. parseInt ("23ab") returns 23.
However, it is not always as simple as the above. Let's look at an example:
The return value of parseInt ("0x12") is 18, rather than the number before the first string is returned, if the string starts with "0x", you must note that the number after "0x" is considered as hexadecimal, so the return value is 18. If it starts with "0" and is not followed by a character, it will be parsed in decimal format in Google's browser, but in IE's browser it will be parsed in octal format. For example:
ParseInt ("0123") returns 123 in Google Chrome and 83 in IE.
I hope this article will help you design javascript programs.