JavaScript在Android的WebView中parseInt函數轉換不正確
今天被一個問題困擾,有一個頁面在瀏覽器(無論是手機還是PC)上運行良好,而通過WebView就出現了問題,有兩個值得計算始終出錯。於是就通過alert,把這個值顯示出來,發現和瀏覽器上計算的結果差了很多,本來是正數,卻變成了負數。仔細比對發現,其中一部分數字被抹掉了,這些數字都是通過parseInt由字符串轉化而來的。而被抹掉的值,和其他正常的數字唯一區別是,它們都是以0開頭的,比如“04903”,而其他值都是“90874”。如此,原因就很明顯了,WebView支持的`JavaScript的parseInt把以0開頭的字符串全部轉爲了0。問題找到了解決就比較容易了,自己寫一個str2Int的方法,來代替parseInt就可以了。
複製代碼 代碼如下:
str2Int:function(str){
str = ace(/^0+/g, ');
if(th == 0){
return 0;
}
return parseInt(str);
}
相關文章
-
Diaspora Finance Powers Global Development美文欣賞
Diaspora Finance Powers Global DevelopmentLuis Lopez-Ramirez, a 30-year old Honduran immigrant, makes pizza.He has worked two jobs over 16 hours each day.He carefully saves his money and rents a small -
Time To Stop Worrying About Invasive Species美文欣賞
Time To Stop Worrying About Invasive Species?You shouldn’t judge a book by its cover. And you shouldn’t judge a species by its place of origin. So say ecologists in a commentary in the joual Nature. -
英語閱讀:Covergirl unveils empowering brand update
It may have been founded 60 years ago old, but US cosmetics giant Covergirl is proving that you're never too old to reinvent yourself.The Coty-owned makeup brand, which has been attracting attenti -
國中英語寫作範文:My Happy Times During Winter Vacation
寒假期間我經常去看望我的爺爺奶奶。他們都已經70歲了,住在鄉下。我可以在那裏做很多有趣的事情。我習慣一大清早起牀呼吸鄉下的新鮮空氣。下雪後,我喜歡跟同伴們一起滑雪滑冰。夜幕降臨時,我就坐在火爐邊,聽奶奶講動聽的 -
私家車的壞處The Disadvantages of Private Cars英語作文
The Disadvantages of Private CarsAs the development of the world, our country’s economy has become more and more better, people’s life standard has become much better, so people have money to amuse. -
My Happy Times During Winter Vacation國中英語作文
寒假生活-My Happy Times During Winte寒假期間我經常去看望我的.爺爺奶奶。他們都已經70歲了,住在鄉下。我可以在那裏做很多有趣的事情。我習慣一大清早起牀呼吸鄉下的新鮮空氣。下雪後國小作文 ,我喜歡跟同伴們一起滑 -
Should Private Cars Be Encouraged in China高中英語作文
with the rapid development of economy in china, the number of people who own private cars is rising day after day. quite different views eist as to this e is no doubt that private cars have been playi -
The relationship between theory and practice大學英語作文
We all know that books are a source of there are those who think what the book contains must be is stupid to follow the books without any the old saying goes,to believe everything in books is worse -
英語閱讀:VIT declines to acquire Ang Mo Kio property
像在北方地區大棚內的黑坑較爲多見,自然不會受到多少自然因素的影響,可遮風擋雨,現在的垂釣環境也是越來越好內部還設有地暖、大燈,大大提高了釣魚的舒適度。冬季來臨,氣溫突降,越來越多的北方釣友開始轉戰大棚,高手在線嘉賓 -
關於javascript中encodeURI和decodeURI的使用方法
關於java script中encodeURI和decodeURI的使用方法 一、基本概念encodeURI和decodeURI是成對來使用的,因爲瀏覽器的`地址欄有中文字符的話,可以會出現不可預期的錯誤,所以可以encodeURI把非英文字符轉化爲英文編