Tips for using parseInt in javascript _ basic knowledge

Source: Internet
Author: User
Several times in JavaScript, The parseInt function converts the string to a number. The preceding problem occurs several times before, and then compare it with other methods. To perform client verification on the date format entered in the form, I found a segment of code on the Internet, and used parseInt to determine the year, month, and day. The statement is similar to the following:

......

Else if (parseInt (month) <1 | parseInt (month)> 12)

......

However, for the date that is already correct, it cannot be used here. Later I found out that parseInt actually has two parameters: the first is the value to be converted, and the second is the specified hexadecimal. If the second parameter is not specified, it can only convert 01 to 07 correctly (that is, convert them to 1 to 7), starting from 08, it will be converted according to the rule that "0 starts with the octal number! This is exactly the date I selected in September, which exposes this issue. If I did this in the first half of the year, I would leave a BUG, the customer will not be able to enter the date until the second half of the year.

Therefore, the second parameter is added to all the places where parseInt is used in the function, indicating that it is in decimal format. For example, the above sentence is changed:

......

Else if (parseInt (month, 10) <1 | parseInt (month, 10)> 12)

......


Several times in JavaScript, The parseInt function converts the string to a number. The preceding problem occurs several times before, and then compare it with other methods.
When I go to the page to get a calendar and convert it to a number for comparison, there is always a problem with the month conversion,
When I use

Var num = parseInt (01 );
Var num = parseInt (01 );
...
Var num = parseInt (07 );
Var num = parseInt (08 );
Var num = parseInt (09 );
Var num = parseInt (10 );

Assume that when I perform the preceding conversion,
ParseInt (), the parameter passed in from 1 to 7 is no problem, but when I use parseInt (08), there will be a problem, the converted num = 0

At that time, due to the urgent issue of the project, I did not investigate it in detail.
Now, I have read the parseInt javascript method.

The javascript api is described as follows:
ParseInt Method
Returns an integer converted from a string.
ParseInt (numString, [radix])
Parameters
NumString
Required. String to be converted to a number.
Radix
Optional. Between 2 and 36, it indicates the hexadecimal value of the number saved by numString. If this parameter is not provided, the string prefixed with '0x 'is treated as hexadecimal, and the string prefixed with '0' is treated as octal. All other strings are treated as decimal.

After reading the above api, I learned that when the original parameters I transferred are 01 to 08, the parameters start with '0' and are converted to octal. Because octal values can only represent 0 to 7, the conversion is correct when my parameter is 01 to 07, and it is carried when it is 08, and the conversion is 0. and parseInt (10) can be converted to 10

Therefore, the rule is not to omit the last parameter,

Use parseInt (); the conversion is correct.

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.