How to force addition for javascript instead of concatenation

Discussion in 'JavaScript' started by makamo66, Dec 19, 2012.

  1. #1
    I'm trying to add all of the calorie contents in my javascript like this:


    total = parseFloat(myInt1 + myInt2 + myInt3);
    $('#response').append(total);


    Instead of adding the variables they get concatenated. I've tried using parseInt, parseFloat, and Number but I still just get concatenation and not addition. Please look at the view source at http://maureenmoore.com/momp_112412/121912_800.html
     
    makamo66, Dec 19, 2012 IP
  2. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #2
    try parseFloat(myInt1)+parseFloat(myInt2)+parseFloat(myInt3)
     
    tiamak, Dec 19, 2012 IP
  3. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Thanks I added your code to maureenmoore.com/momp_112412/121912_800.html but it didn't improve the situation.
     
    makamo66, Dec 19, 2012 IP
  4. makamo66

    makamo66 Active Member

    Messages:
    125
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    The problem was solved by axrwkr at stackoverflow:

    stackoverflow.com/questions/13953939/how-to-force-addition-instead-of-concatenation-in-javascript
     
    makamo66, Dec 19, 2012 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    The problem was actually in the code you didn't show us.
     
    Rukbat, Dec 20, 2012 IP