User Guide
Data types 67
Casting to int, uint, and Number
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); // 1 1 1
myBoolean = false;
myUINT = uint(myBoolean);
myINT = int(myBoolean);
myNum = Number(myBoolean);
trace(myUINT, myINT, myNum); // 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 (for example,
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 that has
been truncated. For example, the following string values can be cast into numbers.
trace(uint("5")); // 5
trace(uint("-5")); // 4294967291. It wraps around from MAX_VALUE
trace(uint(" 27 ")); // 27
trace(uint("3.7")); // 3
trace(int("3.7")); // 3
trace(int("0x1A")); // 26
trace(Number("3.7")); // 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")); // 0
trace(uint("ten")); // 0
trace(uint("17 63")); // 0