Shaun Mccran

My digital playground

05
A
P
R
2009

Flex Currency Formatter Example

Whilst developing a new Flex shopping cart I came across an issue in dealing with the formatting of currencies for the payment system. The previous version of the shopping cart had preformatted values stored in the actual database, so I was simply displaying them in the Flex front end.

In this version there is the specific requirement that it is able to handle a multi currency configuration. So I thought I'd dig out the documentation on the Flex currency Formatter.

Firstly create a CurrencyFormatter object, and specify the currency symbol, and any other formatting parameters that you require.

Next create a NumberValidator, as this will validate our entered value as a numeric value, we don't need to try and re-format alpha numeric values.

Then create a form to run the test validation against. I often find it easier to build a test mechanism at the same time. Below is a simple submission form, in the form we just enter the value, click the Button, which fires the Format() function.

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<!-- Simple example to demonstrate the CurrencyFormatter. -->
3<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
4
5 <mx:CurrencyFormatter id="ukFormatter" precision="2"
6 currencySymbol="�" decimalSeparatorFrom="."
7 decimalSeparatorTo="." useNegativeSign="true"
8 useThousandsSeparator="true" alignSymbol="left"/
>

9
10 <mx:NumberValidator id="numVal" source="{priceUK}" property="text"
11 allowNegative="true" domain="real"/
>

12
13 <mx:Panel title="CurrencyFormatter Example" width="75%" height="75%"
14 paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10"
>

15
16 <mx:Form>
17 <mx:FormItem label="Enter U.K. dollar amount:">
18 <mx:TextInput id="priceUK" text="" width="50%"/>
19 </mx:FormItem>
20
21 <mx:FormItem label="Formatted amount: ">
22 <mx:TextInput id="formattedUKPrice" text="" width="50%" editable="false"/>
23 </mx:FormItem>
24
25 <mx:FormItem>
26 <mx:Button label="Validate and Format" click="Format();"/>
27 </mx:FormItem>
28 </mx:Form>
29
30 </mx:Panel>
31
32</mx:Application>

The Format() function checks that the value is numeric, and applies the CurrencyFormatter object to the value. Then we simply assign the newly formatted value back to our Text field in the form.

view plain print about
1<mx:Script>
2 <![CDATA[
3
4 import mx.events.ValidationResultEvent;
5 private var vResult:ValidationResultEvent;
6
7 // Event handler to validate and format input.
8
private function Format():void {
9
10 vResult = numVal.validate();
11
12 if (vResult.type==ValidationResultEvent.VALID) {
13 var temp:Number=Number(priceUK.text);
14 formattedUKPrice.text= ukFormatter.format(temp);
15 }
16
17 else {
18 formattedUKPrice.text="";
19 }
20 }
21 ]]>
22 </mx:Script>

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Dayne's Gravatar I have felt few issues when tried to format currencies. So I think this is an easy method to rectify that kind of issues. I am so happy getting such an effective way to make it simpler. Thank you very much! http://www.qspray.com
# Posted By Dayne | 06/09/15 20:52
dr. david caminer's Gravatar In this version there is the specific requirement that it is able to handle a multi currency configuration. So I thought I'd dig out the documentation on the Flex currency Formatter.
# Posted By dr. david caminer | 03/10/15 07:05
drug abuse's Gravatar I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.
# Posted By drug abuse | 07/10/15 05:51
marketing's Gravatar In this version there is the specific requirement that it is able to handle a multi currency configuration. So I thought I'd dig out the documentation on the Flex currency Formatter.
# Posted By marketing | 12/10/15 02:32
roof cleaning's Gravatar Injury after injury plagued the team all year, but it found a way to keep winning, often coming back from large deficits and squeaking out close wins.
# Posted By roof cleaning | 13/10/15 01:06
Build My List 2.0's Gravatar specific requirement that it is able to handle a multi currency configuration. So I thought I'd dig out the documentation on the Flex currency Formatter.
# Posted By Build My List 2.0 | 05/11/15 22:53
delhi to jaipur cab's Gravatar It will no doubt forever hurt but it’s more about the lost of what we all had. I will forever be grateful to God for the season I just personally would had prefer to have had not such an overwhelming Life Storm which I could no longer control.
# Posted By delhi to jaipur cab | 30/11/15 03:32
delhi to jaipur cab's Gravatar I will forever be grateful to God for the season I just personally would had prefer to have had not such an overwhelming Life Storm which I could no longer control.
# Posted By delhi to jaipur cab | 30/11/15 03:41
auto facebook likes's Gravatar Hi! Thanks for the great information you havr provided! You have touched on crucuial points!
# Posted By auto facebook likes | 14/12/15 23:39
bot views youtube's Gravatar Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging,
# Posted By bot views youtube | 14/12/15 23:42
ringing in the ears treatment's Gravatar I've just decided to create a blog, which I have been wanting to do for a while. Thanks for this post, it's really useful
# Posted By ringing in the ears treatment | 16/12/15 20:54
pure slim cleanse's Gravatar Your blog provided us with valuable information to work with. Each & every tips of your post are awesome.
# Posted By pure slim cleanse | 16/12/15 23:34
sleep study results's Gravatar I've just decided to create a blog, which I have been wanting to do for a while. Thanks for this post, it's really useful
# Posted By sleep study results | 17/12/15 01:02
buy instagram followers's Gravatar I've just decided to create a blog, which I have been wanting to do for a while. Thanks for this post,
# Posted By buy instagram followers | 18/12/15 21:27
American Xarelto Lawyers's Gravatar Thanks for putting this together! This is obviously one great post. Thanks for the valuable information and insights you have so provided here.
# Posted By American Xarelto Lawyers | 18/12/15 21:58
cccam server's Gravatar I want you to thank for your time of this wonderful read!!! I definitely enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog....
# Posted By cccam server | 18/12/15 23:45
Assistant Information's Gravatar Great Post This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this..Thank You!
# Posted By Assistant Information | 20/12/15 21:47
cheap student term papers's Gravatar Firstly create a CurrencyFormatter object, and specify the currency symbol, and any other formatting parameters that you require.
# Posted By cheap student term papers | 25/12/15 22:02
click here's Gravatar I m glad reading your article. But should remark on some general things. The web site style is perfect. the articles is really great...
# Posted By click here | 27/12/15 22:05
click here's Gravatar I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article..
# Posted By click here | 28/12/15 21:52
alexa traffic's Gravatar I read a lot of stuff and i found that the way of writing to clearing that exactly want to say was very good so i am impressed and i like to come again in future....
# Posted By alexa traffic | 29/12/15 22:02
buy youtube video likes's Gravatar I read a lot of stuff and i found that the way of writing to clearing that exactly want to say was very good so i am impressed and i like to come again in future....
# Posted By buy youtube video likes | 30/12/15 22:15
High PR blog comments's Gravatar In this version there is the specific requirement that it is able to handle a multi currency configuration. So I thought I'd dig out the documentation on the Flex currency Formatter.
# Posted By High PR blog comments | 04/01/16 21:49
dot sleep apnea test's Gravatar It was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity...
# Posted By dot sleep apnea test | 06/01/16 21:51
office fitouts's Gravatar It was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity...
# Posted By office fitouts | 07/01/16 02:11
amazon fba seller's Gravatar I have feel that this blog is really have all those quality that qualify a blog to be a one.I wanted to leave a little comment to support you and wish you a good continuation.
# Posted By amazon fba seller | 08/01/16 22:47
Victorino Noval and Manny Pacquiao's Gravatar I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will locate that extremely useful...
# Posted By Victorino Noval and Manny Pacquiao | 11/01/16 01:11
read more here's Gravatar I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank youa for the efforts you have made in writing this article.
# Posted By read more here | 16/01/16 04:05
Kraut's Gravatar Good post but I was wondering if you could write a little more on this subject? I’d be very thankful if you could elaborate a little bit further..
# Posted By Kraut | 17/01/16 22:05
Back to top