Everyone knows about and uses Math.round(), Math.ceil(), and Math.floor(). They’re great, but what if you don’t want to round to the nearest whole number? For instance, you might want to round to the nearest even number, or even a fractional value like the nearest tenth. The following functions make rounding in ActionScript 3 a lot more robust:
package com.joshtynjala.utils { /** * Utility functions for the manipulation of numbers. */ public class MathUtils { /** * Rounds a target number to the nearest multiple of another number. * @see Math#round */ public static function roundToNearest(numberToRound:Number, nearest:Number):Number { return Math.round(numberToRound / nearest) * nearest; } /** * Rounds a target number up to the nearest multiple of another number. * @see Math#ceil */ public static function roundUpToNearest(numberToRound:Number, nearest:Number):Number { return Math.ceil(numberToRound / nearest) * nearest; } /** * Rounds a target number down to the nearest multiple of another number. * @see Math#floor */ public static function roundDownToNearest(numberToRound:Number, nearest:Number):Number { return Math.floor(numberToRound / nearest) * nearest; } /** * Rounds a target number to a specific number of decimal places. * @see Math#round */ public static function roundToPrecision(number:Number, precision:int = 0):Number { var decimalPlaces:Number = Math.pow(10, precision); return Math.round(decimalPlaces * number) / decimalPlaces; } } } /* Copyright (c) 2007 Josh Tynjala Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
I’ve used various versions of this class on many projects, and the functions will always be in my toolbox. You can download the class, and use it under the terms of the MIT License.
Im having some trouble using the class, even though I import the class, the Flash IDE keeps telling me the method is undefined.
nevermind, figured it out.
Well I can’t figure it out, wtf was the solution
ok here, so if I create all these folders and call the ActionSript text file MathUtils.as
first import
import com.joshtynjala.utils.*;
then usage is
MathUtils.roundDownToNearest(3.3993, 4)
i forgot to include MathUtils. before the function names cuz i’m not into packaging OOP yet.
Sorry guys, I didn’t mention that there’s some minor setup needed on your part. For one, you need to put the file into a directory com/joshtynjala/utils and usage is as Detrus describes.
Tnx!
Thanks a lot for the roundToPrecision function!
Cheers! Google found me here after a search for FLASH ACTIONSCRIPT ROUND 10 and whilst I did not need the Class entirely, the method for rounding to a nearest number was priceless. So simple if you know the math.
Brilliant. THANKS!
Why not just use .toFixed() ?
Ah yes, you’re right. toFixed() does the same thing as my roundToPrecision() function. I wasn’t aware of that one.