1 Reply Latest reply on Jun 16, 2010 7:58 AM by oldMster

    Round Half To Even in ActionScript

    Sumanta150374

      How to do Round Half To Even (ROUND_HALF_EVEN in Java) in Flex? I have tried with 'NumberBaseRoundType.NEAREST' but does not work. Can somebody please help me in this?

        • 1. Re: Round Half To Even in ActionScript
          oldMster Level 3

          I don't see a function built in to Flex, but the algorithm isn't all that hard.  There are multiple ways to do it, and someone else probably has a better algorithm, but this one should work:

           

          public function roundHalfToEven(value:Number,decimals:uint):Number {

          var rounded:Number = new Number();

          if (decimals > 0) {

             decimals=decimals*10

          }

          else {

             decimals=1

          }

          s value=value*Number(decimals) + .5

          if (value%1 == 0 && value%2 != 0 ) {

             //it was exactly half, and got rounded up to an odd number, so take it down

             value=value-1

          }

          rounded = Number(int(value)) / Number(decimals)

          return rounded

          }