I have a form with a list of input boxes. The first of which has the
price of the item in a hidden field, the second of which is a textbox
with a default value of 0. This script is supposed to loop through all
of the text boxes two at a time and multiply the text box pointed to by
I (the text box quantity field) by the text box pointed to by i-1 (the
price). This works fine except for when it adds three items together at
�5.25, �5.60 and �4.55 when I get 15.399999999999998 instead of 15.40.
Can anyone see why?
Thanks
function calculateTotal(textBox){
if(isNaN(textBox.value)){
alert("Please only enter numbers into the quantity
field")
textBox.value = 0;
}
var formTotal = 0;
for(i=1;i<textBox.form.elements.length-3;i += 2){
formTotal +=
parseFloat(textBox.form.elements[i-1].value) *
parseInt(textBox.form.elements[i].value);
}
formTotal += ''
if(formTotal.indexOf('.') == -1){
formTotal += '.00';
} else {
if(formTotal.length - formTotal.indexOf('.') == 2){
formTotal += '0';
}
}
textBox.form.total.value = formTotal;
}
Giles Roadnight
http://giles.roadnight.name
--
** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
For human help, e-mail: [EMAIL PROTECTED]