How to use NumberFormatter?

Posted on 2009-02-18
Last Modified: 2013-11-11

In the help fiile of NumberFormatter I read for the property 'decimalSeparatorFrom': Decimal separator character to use when parsing an input String.

In what case does the NumberFormatter class "parses an input string"?
I thought NumberFormatter is used to format (=output) Numbers.

Could someone provide me a clear example of the usage of the NumberFormatter where both the properties 'decimalSeparatorFrom' and 'decimalSeparatorTo' are used?
(use let's say "," and "." for them)

Question by:zzynx
    LVL 37

    Accepted Solution

    It's ok. I found it myself.

    I attach a little app to demonstrate the usage of NumberFormatter for the people stumbling upon this PAQ.
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx=""
    		import mx.formatters.NumberFormatter;
    		private var _formatter:NumberFormatter = new NumberFormatter();
    		private function onComplete():void {
    			_formatter.decimalSeparatorFrom = "."; // In fact obsolete, since that's the default
    			_formatter.decimalSeparatorTo = "#";
    			var nbr:Number = 12.345;
    			var str:String = _formatter.format(nbr);
    			output1.text = "12.345 formatted with _formatter(. to #) = " + str;
    			// Assume that the user enters in a textinput a number in the format 12#345
    			// You want to create a Number of it
    			// Then you have to format it in the format the Number constructor accepts (=12.345)
    			_formatter.decimalSeparatorFrom = "#";
    			_formatter.decimalSeparatorTo = ".";  
    			output2.text = str + " formatted with _formatter(# to .) = " + _formatter.format(str);
    			// In this format you can successfully feed it to the Number's contructor:
    			nbr = new Number(_formatter.format(str));
    			output3.text = nbr.toString();
    		<mx:Label id="output1" />
    		<mx:Label id="output2" />
    		<mx:Label id="output3" />

    Open in new window

    LVL 19

    Expert Comment

    by:Gary Benade
    very good question. I never could find the parse methos of the numberformatter either, it's not in the source code, so I do this:

    				var f:NumberBase = new NumberBase();
    				f.decimalSeparatorFrom = ",";
    				f.thousandsSeparatorFrom = ".";
    				var s:String = "1.234.560,01"; 
    				trace( f.parseNumberString( s)); // 1234560.01 
    				var nf:NumberFormatter = new NumberFormatter();
    				nf.decimalSeparatorTo = ".";	
    				nf.thousandsSeparatorTo = ",";	
    				trace( nf.format( 1234560.01)); // 1,234,560.01

    Open in new window


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
    In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
    The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
    This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now