4 Replies Latest reply on Feb 12, 2007 9:31 AM by Newsgroup_User

# function to format a number to certain decimals?

what is the function to format a number to certain decimals?

• ###### 1. Re: function to format a number to certain decimals?
try searching this forum. that's been answered more than once.
• ###### 2. Re: function to format a number to certain decimals?
would not it be shorter just to write the function name?
news:eqo1c7\$gut\$1@forums.macromedia.com...
> try searching this forum. that's been answered more than once.

• ###### 3. Re: function to format a number to certain decimals?
Hi there

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));
• ###### 4. Re: function to format a number to certain decimals?
Thanks