Incorrect parseInt Function Conversion in Android WebView using JavaScript Solution

Source: Internet
Author: User

Incorrect parseInt Function Conversion in Android WebView using JavaScript Solution

Incorrect parseInt Function Conversion in Android WebView using JavaScript Solution
This article mainly introduces how to solve the incorrect parseInt Function Conversion problem in the WebView of Android, because the converted string numbers all start with 0, as a result, the parseInt function has different conversion results in the browser and Android WebView. This article provides a solution. For more information, see

Today, I was troubled by a problem. One page runs well on a browser (whether on a mobile phone or a PC), and a problem occurs through WebView. There are two problems worth computing. Then, through alert, this value is displayed, and it is found that the calculation result is much different from that on the browser. Originally, it is a positive number, but it turns into a negative number. After careful comparison, we find that some of the numbers are erased, and these numbers are converted from strings through parseInt. The only difference between erased values and other normal numbers is that they all start with 0, for example, "04903", while other values are "90874 ". The reason is obvious. The JavaScript parseInt supported by WebView converts all strings starting with 0 to 0. It is easier to solve the problem. You can write a str2Int method instead of parseInt.

The Code is as follows:

Str2Int: function (str ){

Str = str. replace (/^ 0 +/g ,'');

If (str. length = 0 ){

Return 0;

}

Return parseInt (str );

}

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.