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);
}