We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How to use NumberFormatter?

zzynx
zzynx asked
on
Medium Priority
902 Views
Last Modified: 2013-11-11
Hi,

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)

Thanks
Comment
Watch Question

Sr. Software engineer
CERTIFIED EXPERT
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="http://www.adobe.com/2006/mxml"
	creationComplete="onComplete()">
 
	<mx:Script>
	<![CDATA[
		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:Script>
 
	<mx:VBox>
		<mx:Label id="output1" />
		<mx:Label id="output2" />
		<mx:Label id="output3" />
	</mx:VBox>
	
</mx:Application>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.