Flex Currency Converter

This is a quick post with some source code that may be of use to someone. I needed to format the Number 560 (a figure representing total pence) into pounds and pence £5.60. Obviously I needed to ensure that the decimal was in the correct place and if two digits were passed that I would have to format it appropriately to precede the digits with a zero and a decimal place i.e. £0.60. Here is the source to the 2 very small classes I use to achieve this via a static method like so:

1
2
3
4
5
6
7
8
private var exampleOne:Number = 560; // Expect £5.60
private var exampleTwo:Number = 50;  // Expect £0.50
          
public function testCurrency():void
{
      trace( CustomPriceFormatter.getPrice( exampleOne.toString(), "£" ) );
      trace( CustomPriceFormatter.getPrice( exampleTwo.toString(), "£" ) );
}

Comments