4 Replies Latest reply on Mar 1, 2010 10:11 PM by Arun Lal

    Use NumberFormatter.

    quoc_thai Level 1

      Hi all,

         I have number is : 5426. And I want convert to 54,26. How I use NumberFormatter to convert this number ?

       

      Thanks.

        • 1. Re: Use NumberFormatter.
          Gregory Lafrance Level 6

          How did you end up with a number that is 5426 but should be 54,26? I ask that because it seems like some other area of your program or backend may not be treating data correctly.

           

          If you simply have a number 5426 and you wish to change it to 5426, just divide by 100. If you end up with something like 54.2600, and you only want precision of 2, the use a NumberFormatter, specifying 2 for the precision property.

           

          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Script>
              <![CDATA[
                private function formatNumber():void{
                  var num:Number = Number(myLabel.text)/100;
                  myLabel.text=nf.format(num)
                }
              ]]>
            </mx:Script>
            <mx:NumberFormatter id="nf" precision="2"/>
            <mx:Label id="myLabel" text="5426" fontSize="30"/>
            <mx:Button label="Format Number" click="formatNumber();"/>
          </mx:Application>
          
          


          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          1 person found this helpful
          • 2. Re: Use NumberFormatter.
            quoc_thai Level 1

            Thank for reply,

                 If divide by 100, it's very simple! But my database have some thousand of records. It is 5426 and when show at ADG, it should be 54,26. I tried used NumberFormatter but I can't do this !

            • 3. Re: Use NumberFormatter.
              rickcr Level 1

              quoc_thai wrote:

                   If divide by 100, it's very simple! But my database have some thousand of records. It is 5426 and when show at ADG, it should be 54,26. I tried used NumberFormatter but I can't do this !

               

              What does your database have to do with it? Did you try what was mentioned that you said was simple.. divide by 100 first then format? I'm probably misunderstanding your question.

              • 4. Re: Use NumberFormatter.
                Arun Lal

                If you need a Thousand separator the use this logic

                 

                               var nf:NumberFormatter = new NumberFormatter();
                               nf.useThousandsSeparator = true;
                               var result:String = nf.format(5426)

                 


                               // result -> 5,426

                 

                If you need a hundred separator then u need to write your own logic something like this:

                 

                                var num:String = (Number(txtArea.text)/100).toPrecision(2);
                                var num1:String = (Number(txtArea.text)%100).toString();
                                var result:String = num+","+num1

                 

                               // result -> 54,26

                 

                Let me know if this helped you

                1 person found this helpful