2 Replies Latest reply on Feb 7, 2012 2:21 PM by DonMitchinson

    ActionScript rounding issue

    Jason Stamp UK Level 1

      I am writing a calculation engine as a re-usable Flex library in Flash Builder. As part of of this engine I need to round calculations to certain decimal places, but seem to be having some rounding issues with certain numbers. As an example if I have a number of 1.275, it always rounds down to 1.27, when I need it to round to 1.28 when using 2 decimal places.


      var a:Number = 1.275;

      a.toFixed(2) // returns a value of 1.27

      Math.round(a * 100) / 100 // returns a value of 1.27


      In other instances, e.g. 1.125 rounds up to 1.13.


      When looking into the manual rounding equation the problem seems to lie when we multiply a by 100:


      a * 100 // returns a value of 127.49999999999999


      This explains why a rounds down to 1.27.


      Is there any solution to get 1.275 to round to 1.28?


      Any advice would be appreciated.





        • 1. Re: ActionScript rounding issue
          GordonSmith Level 4

          I don’t know how to do the type of decimal rounding you’re looking for, because Numbers aren’t stored in decimal; they’re stored using a binary mantissa and exponent. This means that a number like 1.27 cannot be stored exactly, because as a binary fraction it doesn’t terminate after a finite number of binary digits. What you need is a library for doing decimal math.


          Gordon Smith, Adobe

          • 2. Re: ActionScript rounding issue
            DonMitchinson Level 2

            If you always know the number of decimal places you want to return

            add to the number a rounding factor equal to 0.1 * 10 ^ ((numDecPlaces-1) * -1)


            Before using a.fixed(numDecPlaces)


            A hack that might do the job???