How to use NumberFormatter?


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)

LVL 37
zzynxSoftware engineerAsked:
Who is Participating?
zzynxSoftware engineerAuthor Commented:
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

Gary BenadeCommented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.