JavaScript Patterns 2.8 Number Conversions with parseInt()

王朝学院·作者佚名  2016-05-23
窄屏简体版  字體: 小  |  中  |  大  |  超大  

javaScript Patterns 2.8 Number Conversions with parseInt() 2014-05-23 12:28 by 小郝(Kaibo Hao), ... 阅读, ... 评论, 收藏, 编辑 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 8Number("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.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航