Skip to content
Mar 6 / Sidney de Koning

How-To: The Big ‘Rounding Numbers’ Guide in Actionscript

I wrote so i don’t forget anymore, maybe i’ll help some else with this aswell, its all about rounding numbers, decimals and precision digits in Actionscript.

In AS2 we could use the following to round of number:

Three decimals:

var yourNumber:Number = 23.263636453737383838383838;
yourNumber =  Math.round(yourNumber *1000)/1000;
// Outputs 23.263

Two decimals:

var yourNumber:Number = 23.263636453737383838383838;
yourNumber = Math.round(yourNumber *100)/100;
// Outputs 23.26

One decimal:

var yourNumber:Number = 23.263636453737383838383838;
yourNumber = Math.round(yourNumber *10)/10;
// Outputs 23.2

And if you want to do it even nicer you do would write a little function so you dont have to do it manually all the time:

function round(num:Number, precision:Number):Number
{
	var decimalPlaces:Number = Math.pow(10, precision);
	trace(Math.round(decimalPlaces * num) / decimalPlaces)
	return Math.round(decimalPlaces * num) / decimalPlaces;
}
round(2.5678, 2); // returns 2.56

In AS3 we would write:

function round(num:Number, precision:int):Number
{
	var decimalPlaces:Number = Math.pow(10, precision);
	trace(Math.round(decimalPlaces * num) / decimalPlaces)
	return Math.round(decimalPlaces * num) / decimalPlaces;
}
round(2.500078, 2);

Gives us back a different result, namely 2.5 because flash removes the zero at the end of decimal values. This is ok if we use this result to calulate with. But if we wanted to use the result of this function as a visual asset, to make use in a textfield for instance, then we have to re-write this function so that it returns a string (in this case 2.50).

function round(num:Number, precision:int):String {
	var intVal:Number = Math.floor(num);
	var finalLength : int = intVal.toString().length + 1 + precision;
	var baseString : String = num.toString().substr(0, finalLength);
	if(baseString.length == intVal.toString().length) {
		baseString += ".";
	}
	while(baseString.length < finalLength) {
		baseString += "0";
	}
	trace(baseString)
	return baseString;
}
round(2.500078, 3); // 2.500

This would return us 2.500.

Onother quick way to do this in AS3 is using the toFixed() method in the Number class.

Number(2.500078).toFixed(3));

So there you have it; three ways of rounding numbers in Actionscript

Please consider to buying me a coffee.

10 Comments

Leave a comment
  1. Thomas Gabrielsen / Jun 13 2009

    Hi Sidney

    Thanks for sharing. I’ve used a method similar to yours. If you pass in a string, it returns all the decimals even if all the decimals are zero’s. If you pass in a Number it returns number.

    public static function removeDecimals( number:*, numOfDecimals:uint):String
    {

    var newNumber:* = String(number);

    var slicePosition:Number = newNumber.lastIndexOf(“.”) + numOfDecimals + 1;

    newNumber = newNumber.slice( 0, slicePosition )

    newNumber = number is String ? newNumber : Number( newNumber );

    return newNumber;

    }

  2. Sidney de Koning / Jun 24 2009

    Hi Thomas,

    Thanks for the reply and code! Very usefull.

    Greets Sid

  3. Hussein / Nov 12 2009

    Thanks you guys for sharing, very useful!

  4. Pieter / Feb 11 2010

    Here’s a simpler and faster way:

    var num:Number = 7.31343;
    trace(num.toFixed(2)); // 7.31;

  5. Aristophrenia / Jul 14 2010

    People – there is a function called – toFixed(…n) – this will return your decimal places to the nth degree.

    Flash 3D is coming……

  6. Sidney de Koning / Jul 14 2010

    Is nobody reading the article and just skimming over the examples? The last example i give is the toFixed().

    Cheers, Sid

  7. opdrachten / Jan 11 2011

    I really enjoyed reading this post, big fan. Keep up the good work and
    please tell me when can you publish more articles or where can I read
    more on the subject?

  8. cyw / May 6 2011

    great, thx for sharing !!!

  9. felipe / Aug 27 2011

    that’s not rounding, that’s truncating

Trackbacks and Pingbacks

  1. Rounding of Numbers in Actionscript – funky monkey blog

Leave a comment