Definition and usage analysis of parseint () function in JavaScript _javascript tips

Source: Internet
Author: User
Tags first string

This example describes the definition and usage of the parseint () function in JavaScript. Share to everyone for your reference. The specific analysis is as follows:

This function can parse a string and return an integer.

Syntax structure:

Copy Code code as follows:
parseint (string, type)

Parameter list:

Parameters Describe
String Necessary. The string to be parsed.
Type Optional. Represents the cardinality of the number to parse, which is popularly said to be a number of systems, such as binary, octal, or hexadecimal. The value is between 2 ~ 36.

Detailed Description:

A. Specify the type parameter:

When the type parameter is specified, the function parses the string according to the specified type parameter, for example:
1.parseInt ("010", 10), which means "010" is a decimal, and the return value is 10.
2.parseInt ("010", 2), means "010" is binary, and the return value is 2.
3.parseInt ("010", 8), 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.
Description: The return value is decimal, type specifies that the first argument is in the system, and the return of the second parameter value is between 2-36 and if not this interval, the parseint function returns a value of Nan. If not all numbers in the string argument, but with other characters, the parseint function returns only the digits before the first character. For example:
parseint ("123ab789", 10) The return value is 123, the first character a after all omitted.

Two. Do not specify type parameters:

When you do not specify a type parameter, the parseint function automatically determines what kind of feed is, in general, decimal, for example:

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

But the situation is not always as simple as the above, to take another example:

parseint ("0x12") returns a value of 18, instead of returning the number before the first string, there is a situation where the string begins with "0x" because the number after "0x" is considered hexadecimal, so the return value is 18. If it starts with "0" and the next is not a character, then this time, in Google Browser will be in decimal resolution, but in IE browser is octal resolution. For example:
parseint ("0123") in Google Browser, the return value is 123, in IE browser return value is 83.

I hope this article will help you with your JavaScript programming.

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.