2 Replies Latest reply on Oct 22, 2010 2:03 PM by BJ9297

# Convert a String to Number

Hi all,

I’m new with Flex 4, and learning

How to convert a formatted string to a number

example “\$54,500.95″ to “54500.95″ in order to do same calculation

any help will be appreciated.

• ###### 1. Re: Convert a String to Number

This might be the solution in case of currency formatter (you can write a utility class and use it)

<code>

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
<![CDATA[
protected function test_clickHandler(event:MouseEvent):void
{
var value1:String = currency.format(a);
var value2:String = currency.format(b);
var num1:String = value1.substr(1,value1.length);
for(var i:int=0; i<num1.length; i++)
num1 = num1.replace(",","");
var num2:String = value2.substr(1,value2.length);
for(var j:int=0; j<num2.length; j++)
num2 = num2.replace(",","");

var total:Number = Number(num1) + Number(num2);
formatNumber.text =  String(total);

}
]]>
</fx:Script>

<fx:Declarations>
<fx:String id="a">5400000000000.56</fx:String>
<fx:String id="b">550000000000.66</fx:String>
<mx:CurrencyFormatter id="currency" precision="2"/>
</fx:Declarations>

<s:layout>
<s:VerticalLayout/>
</s:layout>

<s:Label text="{currency.format(a)}"/>
<s:Label text="{currency.format(b)}"/>

<s:Label id="formatNumber"/>
<s:Button id="test" label="Convert" click="test_clickHandler(event)"/>

</s:Application>

</code>