4 Replies Latest reply on Jul 11, 2007 10:23 AM by skot:317

# round decimal fraction to certain digits after the dot

Is there a function to round a decimal fraction to certain digits after the
dot?
eg.: 3.1416 ---> 3.14

• ###### 1. Re: round decimal fraction to certain digits after the dot
Hi!

I made a function for you. Maybe not the best name, but you can call it what
you want ;-)

function roundToDigit(num:Number, nrOfDigits:Number):Number {
var factor = Math.pow(10,nrOfDigits)
return Math.round(num*factor)/factor;
}
trace(roundToDigit(3.1416,2));

/Jensen/

"aa" <A@aa.com> wrote in message news:e4uiu7\$g2j\$1@forums.macromedia.com...
> Is there a function to round a decimal fraction to certain digits after
> the
> dot?
> eg.: 3.1416 ---> 3.14
>
>

• ###### 2. Re: round decimal fraction to certain digits after the dot
Thanks, Jensen, it works and the name is apt.
Do you mean that there is no built in function in Math object?

• ###### 3. Re: round decimal fraction to certain digits after the dot
Nope, no such function in the Math object (as far as I can see). Note you
also have the Math.ceil and Math.floor functions if you want to round up or
down.

"aa" <A@aa.com> wrote in message news:e4ukae\$hm3\$1@forums.macromedia.com...
> Thanks, Jensen, it works and the name is apt.
> Do you mean that there is no built in function in Math object?
>
>

• ###### 4. Re: round decimal fraction to certain digits after the dot
One problem I found with this is that 22.29683 becomes 22.3, not 22.30. I'm using this to convert for dollar amounts and I need that 0 penny in there.

Any ideas?