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.2Please consider to buying me a coffee.






Nice… very smart code. Lol thanks
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
Hi Kelllan,
Thanks for the function. Very clean and lean. Like it.
Cheers, Sid
Except that this is incorrect rounding. 23.2636 SHOULD round to 23.264!!! I cant believe that actionscript has no decent rounding function !
duuude! thanks!
you’re like helped me so much..
thank you!