Top RIA blogs award

Top RIA blogs

Confessions of an Flash Addict Rotating Header Image

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

3 Comments on “Rounding of Numbers in Actionscript”

  1. #1 Dan
    on Nov 24th, 2008 at 4:59 pm

    Nice… very smart code. Lol thanks

  2. #2 Kellan Fisher
    on Jun 5th, 2009 at 7:36 pm

    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. #3 Sidney de Koning
    on Jun 24th, 2009 at 11:41 am

    Hi Kelllan,

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

    Cheers, Sid

Leave a Comment