JavaScript Patterns 2.8 Number Conversions with parseInt()
2014-05-23 12:28 小郝(Kaibo Hao) 阅读(294) 评论(0) 编辑 收藏 举报Strings that start with 0 are treated as octal numbers (base 8) in ECMAScript 3; however, this has changed in ES5. To avoid inconsistency and unexpected results, always specify the radix parameter:
var month = "06", year = "09"; month = parseInt(month, 10); year = parseInt(year, 10);
Alternative ways to convert a string to a number include:
+"08" // result is 8 Number("08") // 8
These are often faster than parseInt(), because parseInt(), as the name suggests, parses and doesn't simply convert. But if you're expecting input such as "08 hello", parseInt() will return a number, whereas the others will fail with NaN.
作者:小郝
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/haokaibo/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。