3

im trying to calculate currency but when i try it calculates only first number (1.25$+1.25$ returns 2$) this is the code that i made <-- done

var table = document.getElementById("table"), sumVal = 0;

for (var i = 1; i < table.rows.length; i++)
{
    sumVal = sumVal + parseInt(table.rows[i].cells[3].innerHTML);
}

document.getElementById("val").innerHTML =  +sumVal  + "€";
console.log(sumVal);

what should i add or edit so i can calculate entire value of that row and return 2 dollars and 50 cents

3 Answers3

1

I guess parseInt will translate your 1.25 value to 1, try parseFloat

MarcC
  • 413
  • 3
  • 12
1

replace parseInt with parseFloat

apoteet
  • 742
  • 5
  • 21
1

As others have said, use parseFloat instead of parseInt, but you should also multiply those numbers by 100 and then divide your answer by 100 as you don't want to run into floating point errors.

markb
  • 281
  • 2
  • 8