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?

on
Medium Priority
902 Views
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

## View Solution Only

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"?>
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>
``````

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

Commented:
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
``````
##### Thanks for using Experts Exchange.

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