Search code examples
flex4numberformatter

flex 4.1 NumberFormatter: configure to show precision of one only if the number is float


Currently I use the following NumberFormatter:

<mx:NumberFormatter id="numberFormatter" precision="1" useThousandsSeparator="true"  />

so it changes 5.43234234 to 5.4.

I want the NumberFormatter to not show any precision if there isn't one.

which means that if the number is 5.0, i want it to format it to 5, and not to 5.0.

how can I do so?

using flex 4.1

thanks


Solution

  • I think you need to integrate your own actionscript code, you can't do it with a MXML tag only. Something like:

    private function toPrecisionOrRound(number:Number, precision:int):String {
      String result = number.toPrecision(int);
      String rounded = number.toFixed(0);
      if (Number(result) == Number(rounded)) {
        // they are equal so the toPrecision must have zeros at end
        return rounded;
      } else {
        return result;
      }
    }
    

    I haven't run this but I reckon it will work.