JavaScript in WebView of Android the parseInt function transforms the problem solution incorrectly

  • 2020-06-01 08:11:34
  • OfStack

Today, I was troubled by a problem. One page worked well in the browser (whether it was on the phone or on PC), while the problem occurred with WebView. There were two worthy calculations that were always wrong. So I'm going to use alert, and I'm going to display this value, and I'm going to find that it's a lot different from what I calculated in the browser, and it's going to be a positive number, but it's going to be a negative number. A close comparison revealed that part 1 of the Numbers had been erased, having been converted from a string by parseInt. The only difference between the erased value and the normal number is that they all start with a 0, like "04903", while the other values are "90874". For obvious reasons, WebView supports JavaScript's parseInt, which converts all strings beginning with 0 to 0. Once the problem is solved, it is easier to write a method of str2Int instead of parseInt.

    str = str.replace(/^0+/g, ''); 
    if(str.length == 0){ 
        return 0; 
    return parseInt(str); 

Related articles: