Advanced ActionScript Rounding Functions

by Josh Tynjala

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.

About the Author

Josh Tynjala is an indie game developer, entrepreneur, Flash and Flex mercenary, and bowler hat enthusiast.

Discussion
  1. Im having some trouble using the class, even though I import the class, the Flash IDE keeps telling me the method is undefined.

    posted by aaron on 07.19.2007
  2. nevermind, figured it out.

    posted by aaron on 07.19.2007
  3. Well I can’t figure it out, wtf was the solution

    posted by Detrus on 07.27.2007
  4. 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.

    posted by Detrus on 07.27.2007
  5. 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.

    posted by Josh Tynjala on 07.28.2007
  6. Tnx!

    posted by jason on 01.30.2008
  7. Thanks a lot for the roundToPrecision function!

    posted by Theo Lagendijk on 05.19.2008
  8. 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!

    posted by adam on 12.09.2008
Share Your Thoughts

To display code in comments: <pre>Code here. May be multiline. Format XML with &gt; and &lt; entities.</pre>

Some HTML allowed in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>