Definition and Usage Analysis of parseInt () function in javascript, javascriptparseint

Source: Internet
Author: User

Definition and Usage Analysis of parseInt () function in javascript, javascriptparseint

This article describes the definition and usage of the 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:
Copy codeThe 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.