Rounding numbers in Flex using the NumberFormatter class

I was looking at the NumberFormatter class this evening and made this little demo that shows the four different types of rounding (nearest, up, down, and none) in action.

The following example shows how you can use the static NumberFormatter class in conjuction with the NumberBaseRoundType constants to round and format numbers based on a certain criteria.

<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<mx:Application name="NumberFormatter_rounding_test"
            import mx.collections.ArrayCollection;
            import mx.formatters.NumberBaseRoundType;
            private function button_click(evt:MouseEvent):void {
                textInput.errorString = "";
                if (numberFormatter.error) {
                    textInput.errorString = numberFormatter.error;
                arrColl = new ArrayCollection();
                numberFormatter.rounding = NumberBaseRoundType.NEAREST;
                numberFormatter.rounding = NumberBaseRoundType.UP;
                numberFormatter.rounding = NumberBaseRoundType.DOWN;
                numberFormatter.rounding = NumberBaseRoundType.NONE;
    <mx:ArrayCollection id="arrColl" />
    <mx:NumberFormatter id="numberFormatter"
            rounding="up" />
    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="number:"
                <mx:TextInput id="textInput"
                        maxChars="6" />
                <mx:Button label="format"
                        click="button_click(event);" />
    <mx:DataGrid id="dataGrid"
            rowCount="4" />


View source is enabled in the following example.

9 thoughts on “Rounding numbers in Flex using the NumberFormatter class

  1. So, I am having a rounding issue and this app is helping me sort of confirm the issue. Can you explain why if I put it:
    45.68 the rounding UP value is 45.68 (and not 45.69)

    If I put in 17.76 (my problem number in my personal app), the round UP is 17.77 (and not 17.76 like the first example)

    Overall, with a precision of 2, and rounding up, all my numbers in my app round like the first example, 45.68, however 17.76 actually rounds up. Can you explain this??? (Flex 3.5)

    1. @Molaro,

      This is just a guess, but sometimes JavaScript/ActionScript and many other languages store seemingly simple values like “17.76” internally as “17.760000000000005”. So this may be an artifact of the floating point math. So your value of 17.76 has that trailing “0000000000005” which is causing it to get rounded up.

      But again, this is just a guess and I could be very wrong. But for a bit more info on my theory, see


  2. Peter, thanks for the reply. I have seen that before and usually if I trace out the division of the 2 numbers I use to get that number (17.76), I can see that longer value. But in this case I have littler just keyed in the number into my own test code as well as your demo and see the same issue. I’ll keep looking.

    I logged this as a possible bug with Adobe hoping they can provide an explanation if it isn’t a bug.

    Honestly, given that it 17.76, the year of our independence, I sort of assumed some odd developer bug/Easter egg!

    1. @Molaro,

      I don’t think this is an Adobe bug. My understanding is that it is just an unforunate side effect of floating point math.
      That said, I found the following bugs filed against Flash Player on similar topics:

      As well as the following helpful links:


  3. I am looking a textInput Field in which I can restrict first three numaric values and further three alphabet in maxChar 6.
    Please send me a solution.

    Thanks In Advance.

  4. So, given a NumberFormatter with these settings:

    Why do these numbers round to .51 as intended:
    private var numString1:String = “5.5050”;
    private var numString2:String = “85.5050”;
    private var numString3:String = “485.5050”;

    But this number rounds incorrectly to .50 ???
    private var numString4:String = “8485.5050”;

    More importantly, how does one work around this, when the popular *1000 / 1000 trick doesn’t seem to work?

Comments are closed.