I was working to a flex project and I just need to make a cast to a value to int … this it’s simple … and not so.
After few hour later, I have a vision
…
I just try to make a cost from a decimal value to int, and I have noticed that depends on how it’s made … those are the conclusions:
var tmp:int = 57.83 as int // return 0 var tmp:int = 0; tmp = 57.83 as int //return 0 var tmp:int = int(57.83); //retun 57 var tmp:int = 0; tmp = int(57.83); //return 57
When you make a cast to int, ActionScript evaluates the string that you give (ie: 57.83 as int) and if all chars are numbers will return the number as an int, but if you use int method takes the integer from the expression you give (this is similar to math.floor).
If there is something that I missed or it’s not explained properly, please comment.
Tags: ActionScript, int, typecast
This post was written by Stelian Crisan
Views: 7748










I had a similar problem with XML data loaded in and numbers vs strings:
var splashString:String = xml.year.value as String;
Is null / empty.
is okay because it contains non-integer characters.
Fix:
var splashString:String = xml.year.value.toString(); //grr
Its the same issue when you converting long-int , like 100000000000000 it will give you different after conversion
Any solution with this issue?
The difference in how the cast, int() and the ‘as’ operator work. ‘as’ assumes that the value is an instance of what it is being cast to, ie, that 57.83 is at least an int. If it isn’t an int, or a descendant of int, it will return null. In this case, 57.83 is a Number, which isn’t an int or a descendant of int, so ‘57.83 as int’ returns null because 57.83 isn’t an int.
On the other hand, the call to int() will explicitly try to convert the value to an int.
There’s a good discussion of the differences here:
http://stackoverflow.com/questions/996478/actionscript-is-there-ever-a-good-reason-to-use-as-casting
From the adobe manual: “The inclusion of a decimal point causes uint() and int() to return an integer, truncating the decimal and the characters following it.” (http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_12.html)
I fixed this by type-casting as Number, not int (eg. Number(‘1.25′))