Click to See Complete Forum and Search --> : Number problem


bddosch
06-16-2003, 10:49 AM
function getTotal() {
var total;
total = 16.64 + 42.64 + 61.36 + 16.64 + 16.64 + 66.56;
document.Order.total.value=total;
}

The above function results in:

220.48000000000002

it should be

220.48

Why, does it put so many decimals in..

pyro
06-16-2003, 11:06 AM
Try:

function getTotal() {
var total;
total = 16.64 + 42.64 + 61.36 + 16.64 + 16.64 + 66.56;
document.Order.total.value=total.toFixed(2);
}

Khalid Ali
06-16-2003, 11:07 AM
Here in all generation 6+ browsers

function getTotal() {
var total;
total = 16.64 + 42.64 + 61.36 + 16.64 + 16.64 + 66.56;
document.Order.total.value=total.toFixed(2);
}

bddosch
06-16-2003, 11:17 AM
This works fine, thank you. Do you know of a site were I could learn more cool little things like this to play arround with in javascript?

pyro
06-16-2003, 11:18 AM
You might want to look at the documentation: http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/

EDIT: you won't, however, find toFixed(x) in there, as that is javascript 1.5