Here ??™ s a quick look at using these conversion methods.
Enter the following into a new file and save it as TypeConversion.hx :
class TypeConversion
{
static public function main()
{
var myInt : Int = 45;
(continued)
Part I: The Core Language
40
var myFlt : Float = 0;
var myStr : String = ??????;
myStr = Std.string( myInt );
trace( myStr );
// Outputs: 45
myFlt = Std.parseFloat( myStr ) / 2;
trace( myFlt );
// Outputs: 22.5
myInt = Std.int( myFlt );
trace( myInt );
// Outputs: 22
}
}
Having set up the initial variable values, the integer value 45 stored in myInt was converted to type
String and passed to the variable myStr . Next, the value of myStr was converted to the Float type,
which would have resulted in 45.0. However, to make the conversion more evident, it was divided by 2,
resulting in the value 22.5. The last conversion was to then convert the floating point value back to an
integer, which lost the trailing .5 leaving 22.
Comparing the Type of a Value
Very often, particularly when dealing with values stored in a Dynamic variable, you will need to know
the type of a variable ??™ s value before you can act upon it.
Pages:
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114