Click to See Complete Forum and Search --> : limiting decimal places


damon2003
12-09-2003, 04:50 PM
Hi,
I am doing some simple calculation with some numbers obtained from some textfields.
The values are in the form 45.00, 50.02, 100.00, ect.

When I perform a simple multiplication I am sometimes getting far too many decimal places that I want, I think it is is a floating point number. Is there a way to limit how many decimal places my answers have?
thanks

AdamBrill
12-09-2003, 10:07 PM
You can use toFixed(2) to only allow only 2 decimal places. Here is a simple example:

num = 123.4567;
num = num.toFixed(2);
alert(num);

This will alert the number with only two decimal places.

damon2003
12-10-2003, 07:34 AM
Hi,

I have discovered an issue with this. It doesnt seem to work on a mac. In fact not only does it not work, it messes up the entire script.

How can I get round this. I would still like to use this for browsers that support it. Will the following prevent a mac from running this part of the script if ti doesnt support this function?

<!--
//-->

thanks a lot

AdamBrill
12-10-2003, 08:19 AM
No, that won't hide it, but try seeing if this works. Basically it is the equivalent without using toFixed():

num = 100.123456;
num = Math.round(num*100)/100;
alert(num);

Let me know if that works better.