Skip to content
Oct 15 / Sidney de Koning

Rounding of Numbers in Actionscript

[UPDATE: I have an updated version for both Actionscript 2 and Actionscript 3 here]
To truncate Numbers in Actionscript:
Three decimals:

var yourNumber:Number = 23.263636453737383838383838;
yourNumber =  Math.round(yourNumber *1000)/1000;
// Outputs 23.263

Two decimals:

var yourNumber:Number = 23.263636453737383838383838;
yourNumber = Math.round(yourNumber *100)/100;
// Outputs 23.26

One decimal:

var yourNumber:Number = 23.263636453737383838383838;

yourNumber = Math.round(yourNumber *10)/10;
// Outputs 23.2

Please consider to buying me a coffee.

5 Comments

Leave a comment
  1. Dan / Nov 24 2008

    Nice… very smart code. Lol thanks

  2. Kellan Fisher / Jun 5 2009

    function decRound(num,dp)
    {
    var decimals=Math.pow(10,dp);
    var output=Math.round(num*decimals)/decimals;
    return output;
    }

    // num is the number you wish to round
    // dp is the amount of decimal places to round it too

  3. Sidney de Koning / Jun 24 2009

    Hi Kelllan,

    Thanks for the function. Very clean and lean. Like it.

    Cheers, Sid

  4. Grant Smith / Feb 25 2011

    Except that this is incorrect rounding. 23.2636 SHOULD round to 23.264!!! I cant believe that actionscript has no decent rounding function !

  5. elor / Jun 2 2011

    duuude! thanks! :D
    you’re like helped me so much..
    thank you!

Leave a comment