Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Strange problem adding variables

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    next time you post a code could you please post a working example of your fault.
    Code:
    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) {

  • #3
    New to the CF scene
    Join Date
    Feb 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by dyingjoy; 11-11-2004 at 09:12 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •