SoFunction
Updated on 2025-03-01

JavaScript parseInt function conversion in Android WebView solution

Today I was troubled by a problem, there was a page that worked well on the browser (whether it was a phone or a PC), and there were problems with WebView, and there were two worth computing and always errors. So I displayed this value through alert, and found that it was much different from the result calculated on the browser. It was originally a positive number, but it turned into a negative number. After careful comparison, 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 the other values ​​are "90874". This makes it obvious that the parseInt of JavaScript supported by WebView converts all strings starting with 0 to 0. It is easier to solve the problem after finding a solution. Just write a str2Int method to replace parseInt.

Copy the codeThe code is as follows:

str2Int:function(str){ 
    str = (/^0+/g, ''); 
    if( == 0){ 
        return 0; 
    } 
    return parseInt(str);