syntax :
Number(yourStringVariable)
keep in mind that your string may not always be able to be cast into a
number, so you may want to add a conditional to check.
here is some sample code to demostrate casting a string into a number,just
call the function and you'll see it alert 100 :
<mx:Script>
<![CDATA[ function castTest(){
var myString:String = "60"; var total:Number; total = 40 + Number(myString); alert(total.toString()); } ]]>
</mx:Script> Without the Number( .... ) cast statement, you will get a compile time
error : "Type mismatch in assignment statement: found String where Number
is required." -But remember that you will not get a compile time
error if you are casting to a custom class type, which is why you may want to
add a conditional to test if the cast worked propertly (it will return nullif
the cast cannot be performed) :
<mx:Script>
<![CDATA[ function castTest(){
var myString:String = "60"; var total:Number; total = 40 + Number(myString); if(total){ alert(total.toString()); } } ]]>
</mx:Script> hth
nate nielsen
|
- Re: [flexcoders] How do you convert a string to a number Nate Nielsen
- Re: [flexcoders] How do you convert a string to a nu... Darron J. Schall