Weird NumberFormatter behavior in Flex, AS3

959 Views Asked by At

I came across this weird behavior while using NumberFormatter in AS3.

It's important to understand that I need to use NumberBaseRoundType.UP because I need to use NumberBaseRoundType.DOWN when applying a credit.

Considering the rounding precision that has a value of 2, I guess the NumberFormatter shouldn't change my number. In the example below, 2.43 once formatted become 2.44..!

So the Math.pow(...) is not the solution I'm looking for PLUS, I am very interested to understand what and why this is happening. THANKS!

var roundUp:NumberFormatter = new NumberFormatter();
roundUp.rounding = NumberBaseRoundType.UP;
roundUp.precision = 2;

trace(roundUp.format(2.41)); // Output : 2.41
trace(roundUp.format(2.42)); // Output : 2.42
trace(roundUp.format(2.43)); // Output : 2.44 <-- ???
trace(roundUp.format(2.44)); // Output : 2.44
trace(roundUp.format(2.45)); // Output : 2.46 <-- ???
trace(roundUp.format(2.46)); // Output : 2.46
1

There are 1 best solutions below

2
On

This is not a complete answer. But to start off with which NumberFormatter class are you using (what's the package it comes from). Some of the source is available, some of it is contained in the player itself and isn't accessible, but in looking at the on included in Spark from the 4.6 framework it includes this class level comment that gives some insight:

/**
 *  The NumberFormatter class provides locale-sensitive formatting
 *  and parsing of numeric values. It can format <code>int</code>,
 *  <code>uint</code>, and <code>Number</code> objects.
 *
 *  <p>This class is a wrapper class around the 
 *  flash.globalization.NumberFormatter class. 
 *  Therefore, the locale-specific formatting
 *  is provided by the flash.globalization.NumberFormatter.
 *  However, this NumberFormatter class can be used in MXML declarations,
 *  uses the locale style for the requested Locale ID name, and has
 *  methods and properties that are bindable.  
 *  </p>
 *
 *  <p>The flash.globalization.NumberFormatter class use the
 *  underlying operating system for the formatting functionality and
 *  to supply the locale-specific data. On some operating systems, the
 *  flash.globalization classes are unsupported, on these systems this wrapper
 *  class provides fallback functionality.</p>
 *
 *  @mxml <p>The <code>&lt;s:NumberFormatter&gt;</code> tag inherits all of the tag 
 *  attributes of its superclass and adds the following tag attributes:</p>
 *
 *  <pre>
 *  &lt;s:NumberFormatter 
 *    <strong>Properties</strong>
 *    negativeNumberFormat="<i>locale and OS dependent</i>"
 *  /&gt;
 *  </pre>
 *
 *  @includeExample examples/NumberFormatterExample1.mxml
 *  @includeExample examples/NumberFormatterExample2.mxml
 *
 *  @see flash.globalization.NumberFormatter
 * 
 *  @langversion 3.0
 *  @playerversion Flash 10.1
 *  @playerversion AIR 2.5
 *  @productversion Flex 4.5
 */

Ultimately though it states that the player uses the underlying OS in some way to achieve the formatting. It is certainly a strange problem but I'd be surprised if this wasn't ever caught before and an explanation hasn't been posted or at least a bug report if it is in fact an issue in flash player. Some more details on your SDK version and player version may help, also have you tried fiddling with either of these is there any change in results?

Also depending on what you're attempting to achieve you may be able to just get around the issue by writing your own class to deal with doing the formatting for this case if it's just a one off scenario that you need to deal with this, if it's more of a system wide used thing that needs more of the functionality of the NumberFormatter class I could understand wanting to resolve the underlying issue.