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

    Round Half To Even in ActionScript


      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) {



          else {



          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



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

          return rounded