...

View Full Version : Strange problem adding variables



dyingjoy
11-11-2004, 06:38 PM
The code below works fine with the weight fields holding the following values:
<input name="weight4" type="Text" value="" size="5"> (active4 isn't checked)
<input name="weight3" type="Text" value="33.34" size="5">
<input name="weight2" type="Text" value="33.33" size="5">
<input name="weight1" type="Text" value="33.33" size="5">
The perctotal = 100%

but when I change the field values to
<input name="weight4" type="Text" value="0.01" size="5">
<input name="weight3" type="Text" value="33.33" size="5">
<input name="weight2" type="Text" value="33.33" size="5">
<input name="weight1" type="Text" value="33.33" size="5">
The perctotal = 99.999999999999999998%

I have found a few other combinations that end up with a faulty total, but I can't figure out what is causing the issue.

var perctotal = 0;
if (qform.active4.checked) {
perctotal = (parseFloat(perctotal) + parseFloat(qform.weight4.value));
}
if (qform.active3.checked) {
perctotal = (parseFloat(perctotal) + parseFloat(qform.weight3.value));
}
if (qform.active2.checked) {
perctotal = (parseFloat(perctotal) + parseFloat(qform.weight2.value));
}
if (qform.active1.checked) {
perctotal = (parseFloat(perctotal) + parseFloat(qform.weight1.value));
}
if (perctotal != 100) {
alert("Active questions total " + perctotal + "%\nThe total must equal 100%");
return false;
}


Any ideas as to what is causing this issue?
Thanks,
Doug

Garadon
11-11-2004, 08:07 PM
next time you post a code could you please post a working example of your fault.


perctotal = perctotal + parseInt(document.forms.qform.weight4.value*100,10);

perctotal = perctotal + parseInt(document.forms.qform.weight3.value*100,10);

perctotal = perctotal + parseInt(document.forms.qform.weight2.value*100,10);

perctotal = perctotal + parseInt(document.forms.qform.weight1.value*100,10);

if (perctotal/100 != 100) {

dyingjoy
11-11-2004, 09:07 PM
Sorry about that...I was trying to be concise. I'll include everything next time.

Your solution worked perfectly. Do you have any idea why the other method was causing such strange issues?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum