JavaScript in Android WebView parseint function conversion problem Solving method

Source: Internet
Author: User

JavaScript in Android WebView parseint function Conversion problem Solving method
This article mainly introduces JavaScript in the Android WebView parseint function Conversion problem solving method, because the conversion of the string numbers are starting with 0, resulting in the parseint function in the browser and Android WebView conversion results are not the same, this article gives a solution, the need for friends can refer to the

Today is plagued by a problem, there is a page in the browser (whether mobile phone or PC) on the good run, and through the webview there are problems, there are two worthy of calculation always error. And then, by alert, the value is displayed, and the results of the calculation on the browser are much worse, which is a positive number, but it becomes negative. Carefully compared to the discovery, some of the numbers were erased, and these numbers were transformed by parseint from strings. The only difference between the erased values and the other normal numbers is that they all start with 0, such as "04903", while the other values are "90874". As a result, it is obvious that the webview-supported JavaScript parseint turns the string beginning with 0 to 0. The problem found the solution is easier, write a str2int method, to replace the parseint on it.

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.