Click to See Complete Forum and Search --> : Decimal place


Alv
05-26-2003, 08:24 PM
How do I return a value with only three decimal place? Currently my calculated form value have this value 76437.13500000001. How can I round the value to three decimal place and return them so that user sees 76437.135.

Thanks in advance.

AdamBrill
05-26-2003, 09:00 PM
Try this:

num = parseInt(document.formname.inputname.value);
num = Math.round(test*1000)/1000;

khalidali63
05-26-2003, 09:09 PM
In addition to what adam suggested for browsers supporting javascript 1.5 (all generation 6 + browsers)
you should be able to use a simpler solution
if you have number
var num = 76437.13500000001;

alert(num.toFixed(3))

will return

76437.135

AdamBrill
05-26-2003, 09:57 PM
Khalid, the reason I posted that way is because it is more supported(as you said). Also, with my method, it rounds rather than just cutting it at the third digit. Just wanted to point out the differences so that Alv would know... ;)

khalidali63
05-26-2003, 10:14 PM
sorry if seemed that way,I did not mean to undermine your response in anyway,just wanted the thread owner to know that he has another option too in newer browsers..:-)

jeffmott
05-26-2003, 10:44 PM
Also, with my method, it rounds rather than just cutting it at the third digittoFixed will round to the specified number of digits.

AdamBrill
05-26-2003, 10:56 PM
Originally posted by jeffmott
toFixed will round to the specified number of digits. Ah, so it does. I thought that just cut it rather than rounding... ;)

Alv
05-26-2003, 11:56 PM
Thanks for all the help guys. Appreciate it.

Ice3T
08-13-2003, 10:04 PM
This thread also answered my question. thank you