This really would work in any language, heck even on paper, but the code will be AS3 so figured I’d label it an AS3 post. Short and sweet though…here is how you can round to the nearest number in ActionScript.
function roundToNearest(roundTo:Number, value:Number):Number{
return Math.round(value/roundTo)*roundTo;
}
Essentially you divide by the number you want to round to which will give you a decimal number. Then round that (essentially removing the decimal) and multiple by the rounding number to get you right back to where you were.
Steps:
- roundToNearest(100, 3745);
- 3745/100 = 37.45
- Math.round(37.45) //37
- 37*100 = 3700
That’s it. Hope this helps someone.