Thursday 2 June 2011

AS3 - Round Decimals

To round a decimal number to the a specific decimal point you can use either the 'toFixed' or 'toPrecision' which are Number methods. 



var myNum:int = new int(3.454954849584)
myNum.toFixed(3);
trace(myNum); // 3.454


You can also do precision based decimal rounding with the toPrecision, this will round the last decimal outside the length parameter.


myNum.toPrecision(3);
trace(myNum); // 3.455;


Old-Skool way (does not round chopped off numbers)


2 decimal places:
value = int(( yourNumber ) * 100 ) / 100; 


3 decimal places:
value = int(( yourNumber ) * 1000 ) / 1000; 

No comments:

Post a Comment