码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript Patterns 2.8 Number Conversions with parseInt()

时间:2014-05-26 09:01:52      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

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:

bubuko.com,布布扣
var month = "06",

year = "09";

month = parseInt(month, 10);

year = parseInt(year, 10);
bubuko.com,布布扣


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.

JavaScript Patterns 2.8 Number Conversions with parseInt(),布布扣,bubuko.com

JavaScript Patterns 2.8 Number Conversions with parseInt()

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/haokaibo/p/Number-Conversions-with-parseInt.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!