Home  >  Article  >  Web Front-end  >  Solution to the problem of incorrect conversion of parseInt function in JavaScript's WebView in Android_javascript skills

Solution to the problem of incorrect conversion of parseInt function in JavaScript's WebView in Android_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:02:221729browse

I am troubled by a problem today. There is a page that runs well on the browser (whether it is a mobile phone or a PC), but there is a problem through WebView. There are two worthy calculations that always go wrong. So I used alert to display this value, and found that it was much different from the result calculated on the browser. It was originally a positive number, but it became a negative number. After careful comparison, it was found that some of the numbers were erased, and these numbers were converted from strings through parseInt. The only difference between the erased values ​​and other normal numbers is that they all start with 0, such as "04903", while other values ​​are "90874". In this case, the reason is obvious. The JavaScript parseInt supported by WebView converts all strings starting with 0 to 0. Once the problem is solved, it will be easier. Just write a str2Int method to replace parseInt.

Copy code The code is as follows:

str2Int:function(str){
str = str.replace(/^0 /g, '');
If(str.length == 0){
Return 0;
}  
Return parseInt(str);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn