Tuesday, August 21, 2007

Number handling - Actionscript

Round a number to few decimal places:


To round a number to few decimal places use the following samples

Round to one decimal place
Math.round(35.2499 * 10) / 10 // returns 35.2

Round to two decimal place
Math.round(35.2499 * 100) / 100 // returns 35.25

Round to three decimal place
Math.round(35.2499 * 1000) / 1000 // returns 35.25

Round a number to the nearest multiple of an integer:


To round a number to nearest multiple of an integer use the following samples

Round a number to nearest multiple of 5
Math.round(35.2499 / 5) * 5 // returns 35

Round a number to nearest multiple of 10
Math.round(35.2499 / 10) * 10 // returns 40

2 comments:

Anonymous said...

Thanks - this is exactly what I needed for a project!

Now trying to figure out how to modify this to also round to the nearest 1/8 of a percent (i.e. count 1.125, 1.25, 1.375, etc.)

Anonymous said...

Thanks! :) Couldn't remember the exact formula for nearest multiples...