Groups | Blog | Home
all groups > flash actionscript > february 2007 >

flash actionscript : function to format a number to certain decimals?


kglad
2/11/2007 9:21:11 PM
aa
2/11/2007 10:18:44 PM
what is the function to format a number to certain decimals?

aa
2/12/2007 11:48:43 AM
would not it be shorter just to write the function name?
[quoted text, click to view]

iwantheman
2/12/2007 12:16:48 PM
Hi there:grin;

There is no built in function as such, but the Math object provides the
methods you need to build your own. Basically if you want 2 places you
*multiply by 100
*round it
*divide by 100
for 3 places you use 1000 etc. I include one of mine for you - hope it helps:

var myNumber:Number=12.987654321;
function roundThatNumber(theNumber:Number,thePlaces:Number):Number
{
var theKey:Number=Math.pow(10,thePlaces);
//trace(theKey);
theNumber*=theKey;
//trace(theNumber);
theNumber=Math.round(theNumber);
theNumber/=theKey
return theNumber;
}
trace(roundThatNumber(myNumber,2));
trace(roundThatNumber(myNumber,3));
trace(roundThatNumber(myNumber,4));

aa
2/12/2007 8:39:41 PM
Thanks

AddThis Social Bookmark Button