How do I add instead of concatenate

Asked

Viewed 163 times

2

would like a help.

I have the following code:

 var totalBruto = 0;
        var tot_desconto_vlr = 0;
        var total = 0;
        var totalLiquido = 0;
        $(document).ready(function () {
            $(".desc_vlr").on("input", function () {
                totalBruto = $("#tot_bruto").val();
                tot_desconto_vlr = $(this).val();

                total = totalBruto + tot_desconto_vlr;
                console.info(total);

               var tot_liquido = $(this).attr("desconto");

                $("#" + tot_liquido).val(total);
            });
        });

Instead of summing it, he concatenates it by example: 2 + 2 = 22

I wonder what I’m doing wrong so it can add up instead of concatenate.

From now on, thank you !!

  • Basically .val() returns a string. You have to use parseFloat, Number or parseInt.

1 answer

1


He thinks you have a string. Tries :

total = parseInt(totalBruto) + parseInt(tot_desconto_vlr);
  • Thanks worked out this adding, but it’s rounding up too, example: 29.25 if I inform 1, it rounds to 30 ignoring the decimal places. Knows how to solve?

  • total = parseFloat(totalBruto) + parseFloat(tot_desconto_vlr);

Browser other questions tagged

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