[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




on Nov 24th, 2008 at 4:59 pm
Nice… very smart code. Lol thanks
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
on Jun 24th, 2009 at 11:41 am
Hi Kelllan,
Thanks for the function. Very clean and lean. Like it.
Cheers, Sid