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
 
----- Original Message -----
Sent: Wednesday, March 09, 2005 8:57 AM
Subject: [flexcoders] How do you convert a string to a number

I am just curious because I am trying to do this in Flex

Reply via email to