You can cast any data type into one of the three number types: int, uint, and Number. If Flash Player is unable to convert the number for some reason, the default value of 0 is assigned for the int and uint data types, and the default value of NaN is assigned for the Number data type. If you convert a Boolean value to a number, true becomes the value 1 and false becomes the value 0.
var myBoolean:Boolean = true;
var myUINT:uint = uint(myBoolean);
var myINT:int = int(myBoolean);
var myNum:Number = Number(myBoolean);
trace(myUINT, myINT, myNum); // output: 1 1 1
myBoolean = false;
myUINT = uint(myBoolean);
myINT = int(myBoolean);
myNum = Number(myBoolean);
trace(myUINT, myINT, myNum); // output: 0 0 0
String values that contain only digits can be successfully converted into one of the number types. The number types can also convert strings that look like negative numbers or strings that represent a hexadecimal value (e.g. 0x1A). The conversion process ignores leading and trailing white space characters in the string value. You can also cast strings that look like floating point numbers using Number(). The inclusion of a decimal point causes uint() and int() to return an integer with the characters following the decimal truncated. For example, the following string values can be cast into numbers.
trace(uint("5")); // Output: 5
trace(uint("-5")); // Output: -5
trace(uint(" 27 ")); // Output: 27
trace(uint("3.7")); // Output: 3
trace(int("3.7")); // Output: 3
trace(int("0x1A")); // Output: 26
trace(Number("3.7")); // Output: 3.7
String values that contain non-numeric characters return 0 when cast with int() or uint() and NaN when case with Number(). The conversion process ignores leading and trailing white space, but returns 0 or NaN if a string has white space separating two numbers.
trace(uint("5a")); // Output: 0
trace(uint("ten")); // Output: 0
trace(uint("17 63")); // Output: 0
In ActionScript 3.0, the Number() function no longer supports octal, or base 8, numbers. If you supply a string with a leading 0 to the ActionScript 2.0 Number() function, the number is interpreted as an octal number, and converted to its decimal equivalent. This is not true with the Number() function in ActionScript 3.0, which instead ignores the leading zero. For example, the following code generates different output when compiled using different versions of ActionScript.
trace (Number("044"));
// ActionScript 3.0 output: 44
// ActionScript 2.0 output: 36
Casting is not necessary when a value of one numeric type is assigned to a variable of a different numeric type. Even in strict mode, the numeric types are implicitly converted to the other numeric types. This means that in some cases, unexpected values may result when the range of a type is exceeded. The following examples all compile in strict mode, though some will generate unexpected values:
var sampleUINT:uint = -3; // assign value of type int and Number
trace(sampleUINT); // Output: 4294967293
var sampleNum:Number = sampleUINT; // assign value of type int and uint
trace(sampleNum) // Output: 4294967293
var sampleINT:int = uint.MAX_VALUE + 1; // assign value of type Number
trace(sampleINT); // Output: 0
sampleINT = int.MAX_VALUE + 1; // assign value of type uint and Number
trace(sampleINT); // Output: -2147483648