2007年5月18日星期五

Number() Conversion No Longer Interprets Octals

In ActionScript 1 and 2, when you convert String values into their number equivalents using Number(), if the number was preceded by a "0", the value would be interpreted as an octal value (base 8) much in the same way a preceding "0x" means hex (base 16).

ActionScript Code:

// ActionScript 1 and 2
trace(Number("010")); // 8


This could often cause problems for 'normal' values that, more often than not, you'd rather stay decimal (base 10). In ActionScript 3, this is no longer the case. String values with preceding "0"s are no longer treated as octals (though "0x" still means hex).

ActionScript Code:

// ActionScript 3
trace(Number("010")); // 10


If you want to interpret a string as an octal, you would then use parseInt specifying a radix of 8

ActionScript Code:

trace(parseInt("010", 8)); // 8

0 评论: