2 Replies Latest reply on May 2, 2009 8:46 PM by pquaid

    Numberformatter and scientific notation

    pquaid

      Hello all.  I'm having an issue with Numberformatter that I could use some assistance with.  It looks like this:

       

          <mx:NumberFormatter id="numberFormatter"
              precision="6"
              rounding="up"
              useThousandsSeparator="false"
              useNegativeSign="true"/>

       

      I'm applying it to a Number object that was created from a deserialized java BigDecimal.  In most cases, this is successful. So, 0.000024603877571 becomes 0.000025 and so on.

       

      However, in some cases, my Number is in scientific notation like "0E-15".  In this case, the NumberFormatter formats the number as -15.000000.  Not helpful.  Any ideas on what is going on and how I might could work around it?

        • 1. Re: Numberformatter and scientific notation
          Michael Borbor Level 4

          Probably creating a function to transform the scientific notation will be a

          fast solution, or sending the numbers withouh scientific notation as string.

          • 2. Re: Numberformatter and scientific notation
            pquaid Level 1

            Thanks for the quick response, Michael.  I was about to simply change the sending of the numbers as a string when I decided to try one more thing.  If I use toFixed() on my number first (before the NumberFormatter.format() function) it seems to fix the issue.  So, for example, in my label function for a column of a datagrid that has Numbers that sometimes appear in scientific notation, I do something like this:

             

                        public function myNumberLabelFunction(item:Object, column:DataGridColumn):String
                            var myNumber:Number = item.number;
                            var myNumberString:String = myNumber.toFixed(20);
                            return numberFormatter.format(myNumberString);
                        }

             

            That (toFixed) appears to not mess up the regular numbers as well as the ones that have E notation.  I don't know if anyone has any thoughts on the workaround.  It seems to work find since my formatter is ultimately rounding to a precison of 6.