How to calculate a column with a comma as decimal

Asked

Viewed 47 times

0

In a table that has one of the columns I want to add are with comma as decimal.

tableinserir a descrição da imagem aqui

as I do to add these values?

At the moment I’m using this function but don’t add up the numbers after decimal.

    function sum() {
        var table = document.getElementById("table1"); 
        var sumVal = 0;
        for(var i = 1; i < table.rows.length; i++)

            sumVal = sumVal + parseInt(table.rows[i].cells[3].innerHTML);


       var sumValTest = "Total nao incluido = " + sumVal;
        document.getElementById("val").innerHTML = sumValTest;

        console.log(sumVal);
}
  • You have to replace the comma by a dot.

  • @Sam, you and my hero... perfect thanks for the syntax. How would I move the house of thousands if I have a value of 1,500.50?

  • I updated the answer.

  • 1

    yes of course Augusto, with pleasure. I am novice here. Thank you for the instruction.

1 answer

3


It’s because you need to replace the commas by a dot and change parseInt for parseFloat. Decimals in Javascript are separated by point.

Just make a simple replace:

sumVal = sumVal + parseFloat(table.rows[i].cells[3].innerHTML.replace(",", "."));

You can also remove the thousand points and replace the comma:

sumVal = sumVal + parseFloat(table.rows[i].cells[3].innerHTML.replace(/\./g, "").replace(",", "."));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.