5 Replies Latest reply on May 27, 2009 5:14 AM by Francisc

    toFixed, toPrecision, Math.round

    Francisc Level 3

      Hello (again)!

       

      What is the difference between the 3 mentioned in the subject? ~ toFixed(), toPrecision(), Math.round()

      Also, they return a String value rather than a Number value? ~ toFixed(), toPrecision()

       

      Thank you!

        • 1. Re: toFixed, toPrecision, Math.round
          Michael Borbor Level 4

          toFixed means a n number of places between 0 and 20 after the decimal point.

           

          toPrecision means how many number will include the result .

           

          Yes as the documentation says it returns a string because most of the time

          when you use these functions what you want is to present the data to the

          user.

          • 2. Re: toFixed, toPrecision, Math.round
            Francisc Level 3

            Still don't get it...

             

            toFixed seems to round numbers as well as toPrecision.

            And toPrecision receives as paramerter the number of decimals, same as toFixed.

             

            So all in all, they both seem to do the same thing as Math.round() but return String values.

            • 3. Re: toFixed, toPrecision, Math.round
              Michael Borbor Level 4

              Try this,  the results look quite different to me.

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                   <mx:Script>
                        <![CDATA[
                             import mx.controls.TextArea;
                             private var unknownNumber:Number=Math.PI;
              
                             private var anotherNumber:Number=345.34;
              
                             private function test(num:Number):void{
              
                                  num.toPrecision(2);
                                  num.toFixed(2);
                                  myTA.text="Using toPrecision: " + num.toPrecision(2) + "\n" + 
                                       "Using toFixed: " + num.toFixed(2) + "\n" + "Using round: " + Math.round(num); 
                             }
                        ]]>
                   </mx:Script>
                   <mx:TextArea id="myTA" width="200" height="300"/>
                   <mx:Button label="Test1" click="test(unknownNumber)" />
                   <mx:Button label="Test2" click="test(anotherNumber)" />
              </mx:Application>
              
              1 person found this helpful
              • 4. Re: toFixed, toPrecision, Math.round
                Francisc Level 3

                Guess I was too lazy to do the testing although it would have saved you the trouble of answering.

                It's very clear now. I'll be using toFixed() for this. (the adding thing in a previous post)

                 

                Thank you very much!

                • 5. Re: toFixed, toPrecision, Math.round
                  Michael Borbor Level 4

                  You're welcome.