[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 886
  • Last Modified:

How to use NumberFormatter?

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
0
zzynx
Asked:
zzynx
1 Solution
 
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="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

0
 
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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now