Jump to content

Addition not working


ober
 Share

Recommended Posts

billtotal = 30,000.00 at the start

 

I have this:

var totalboxes = document.getElementById('boxcount').value;
    var billtotal = document.getElementById('billtotalx').value;
    var billtotalbox = document.getElementById('billtotal');
    
    var i=0;
    for(i=1;i<totalboxes;i++)
    {
        if(document.getElementById('bill'+i).value != null)
        {
            billtotal += document.getElementById('bill'+i).value;
            alert(billtotal);
        }
    }

 

I have 8 fields that are being added to an existing total.  This is an onkeyup function and the alert shows this as it loops:

 

30,000.000

30,000.0005

30,000.00050

30,000.000500

30,000.0005000

30,000.0005000

30,000.0005000

30,000.0005000

 

The fields that it is adding contains:

 

0

5

0

0

null

null

null

null

 

So it looks like it is appending instead of adding.  What am I doing wrong??

Link to comment
Share on other sites

I was under the impression that unless you were using strings (which I'm not), it worked fine as an addition.  I also trying wrapping the values in the Number() function and that resulted in a NaN result.

Link to comment
Share on other sites

Anyone else have any ideas?  parseFloat triggers a result of NaN.  Using eval doesn't change anything.

 

Here is the entire function:

 

function calculate_ger(_box)
{
    // ger calculation including totals for GER and Billable units
    var gerbox = document.getElementById('ger'+_box);
    var billbox = document.getElementById('bill'+_box);
    var ratebox = document.getElementById('price_rate');
    var retval = billbox.value * ratebox.value;
    gerbox.disabled = "false";
    gerbox.value = retval.toFixed(2);
    gerbox.disabled = "true";
    
    // billable units total calculation
    
    var billtotal = 0;
    var totalboxes = document.getElementById('boxcount').value;
    var billtotal = parseFloat(document.getElementById('billtotalx').value);
    var billtotalbox = document.getElementById('billtotal');
    
    var i=0;
    for(i=1;i<totalboxes;i++)
    {
        if(document.getElementById('bill'+i).value != null)
        {
            billtotal += parseFloat(document.getElementById('bill'+i).value);
            //alert(billtotal);
        }
    }
    
    billtotalbox.disabled = "false";
    billtotalbox.value = billtotal;
    billtotalbox.disabled = "true";
    
}

 

The first part works great.  I don't understand why the second part isn't.

Link to comment
Share on other sites

WTF...

for(i=1;i<totalboxes;i++)
    {
        if(document.getElementById('bill'+i).value != null && document.getElementById('bill'+i).value != '')
        {
            total += Number(document.getElementById('bill'+i).value);
        }
    }
    alert(billtotal);
    total = total*1 + billtotal*1;
    alert(total);

 

The first alert gives me the number.  The loop is working fine, but when I try to add the two together, I get "NaN".  This is insane!

Link to comment
Share on other sites

Ok... got it figured out.  I was using PHP's number_format() function to format the "billtotalx" value and it was throwing a comma into the value, which was causing it not to be evaluated as a number.  DAMN.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.