Advanced ActionScript Rounding Functions

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 Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. aaron

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

  2. Detrus

    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.

  3. Josh Tynjala

    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.

  4. adam

    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!