16Apr/089
Flash’s Built-in Rounding Function is Severely Limited
Now that I'm working more and more in Flash ActionScript 3 I'll be posting some of my findings about it here.
My first AS3 related post is about the Math.round() function. This function will only round to a whole number (int). What if I have 3.74257444 and need a result of 3.74 instead of what Math.round() would give me: 4?
Well, here's the solution:
function roundDecimal(num:Number, places:Number):Number
{
return int( (num) * Math.pow(10, places) ) / Math.pow(10, places);
}
This function will return the specified amount of decimal places rounded correctly.
Example usage:
var myNumber:Number = 3.74257444;
trace(roundDecimal(myNumber, 2)); //Returns 3.74
trace(roundDecimal(myNumber, 3)); //Returns 3.743
trace(Math.round(myNumber)); //Returns 4
I hope this helps anyone looking for a similar solution.
[via]



April 16th, 2008 - 12:09
Sorry to burst your bubble, but this is built into Flash:
var num:Number = 7.59;
trace(num.toFixed(1));
Result: 7.6
Source: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Number.html#toFixed()
June 18th, 2008 - 18:46
Jeremy you saved my bacon!
Also, FYI poster you really shouldn’t lie about outputs… This function does *NOT* round the numbers at all.
All it does is trim a number to a certain amount of decimals.
toFixed isn’t a very intuitive name for something that rounds decimal numbers, but either way I’m glad I’ve tracked it down.
June 18th, 2008 - 20:43
Wow… “lie” is a pretty serious charge. If I’m mistaken, please show me why, but don’t call me a liar.
A lie is something done on purpose, and if I’m incorrect about this function, it certainly isn’t on purpose. I’m trying to learn this stuff along with everyone else.
August 6th, 2008 - 06:49
Hello,
I just looked at this function and according to their example it does round the numbers;
package
{
import flash.display.Sprite;
public class NumberExample extends Sprite {
public function NumberExample() {
var num:Number = new Number(10.456345);
var str:String = num.toFixed(2);
trace(num); // 10.456345
trace(str); // 10.46
}
}
}
notice the str variable traces a rounded number as a string.
I have not tested this though, they could be wrong.
Neil
September 19th, 2008 - 15:23
the toFixed() function returns a string…that won’t do you any good if you want a number returned…his function will have to be used.
even parseInt(“10.25″) returns 10 and not 10.25.
his roundDecimal function is the perfect solution
thanks for the tip
November 8th, 2008 - 06:12
“even parseInt(”10.25?) returns 10 and not 10.25″
parseFloat(“10.25″);
December 2nd, 2008 - 15:47
This is a nice function! It also works to round to the nearest ten or hundred, etc, by putting a negative number in for the “places” variable, so
roundDecimal(239.4, -1) –> 240
I modified it so that it truly rounds rather than truncates:
function roundDecimal(num:Number, places:Number):Number {
var temp:Number = (num) * Math.pow(10, places);
if(temp%1 < 0.5) return( int(temp)/Math.pow(10, places) );
return( (int(temp)+1)/Math.pow(10, places) );
}
Thanks for the help!!
December 2nd, 2008 - 16:12
@katya:
Glad it was of some use to you!
January 20th, 2009 - 22:08
@ Mark,
Scott wasn’t calling you a liar because your function was wrong – which it is, it trims rather than rounds – it was that you made up the results, in particular:
trace(roundDecimal(myNumber, 3)); //Returns 3.743
Anyone reading this would think that you had executed the above statement and 3.743 was returned. Except it wasn’t – your function returns 3.742. This is very misleading to people reading this blog who have faith that the output that you have written is the actual output from your function. What if every blog writer did this?
Something like this will work:
function roundDecimal(num:Number, places:Number):Number{return Math.round(num * Math.pow(10, places)) / Math.pow(10, places);}