xiaozi 2015-01-05 23:16:45

js中,将数字字符串转换成数字,parseInt不是很好的办法,因为当你忘了传第二个参数的时候,会出现一些问题

~~"42"        //=> 42
~~"042"       //=> 42
~~"the 42"     //=> 0    <- Advantage of using ~~
~~"42px"      //=> 0

+"42"         //=> 42
+"042"        //=> 42
+"the 42"      //=> NaN
+"42px"       //=> NaN

parseInt("42")         //=> 42
parseInt("042")        //=> 34 (Firefox) || 42 (Chrome)
parseInt("09")         //=> NaN (Firefox) || 9 (Chrome)
parseInt("the 42")      //=> NaN
parseInt("42px")       //=> 42  <- Advantage of using parseInt

评论

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.128.0. UTC+08:00, 2024-06-21 09:25
浙ICP备14020137号-1 $访客地图$