Sum returns Nan for values above 999

Asked

Viewed 71 times

4

I have a function that calculates subtotal + ascrescimo - desconto, but I went to test a value above 999 it returns NAN.

//função para calcular o total da nota 
function calcular() {
     var soma1 = 0;
     $(".subtotal01").each(function(indice1, item1){
          var valor1 = parseFloat($(item1).val());
          //console.log(valor);
          if ( !isNaN( valor1 ) ) {
              soma1 += valor1;
          }
     });      
     //pega o valor das desconto01 e caso haja substitue a virgula por ponto
     var acrescimo01 = (document.getElementById("acrescimo01").value).replace(",", ".");      
     acrescimo01=Number(acrescimo01);

     var desconto01 = (document.getElementById("desconto01").value).replace(",", ".");      
     desconto01=Number(desconto01);      


     soma1=(soma1).toFixed(2);
     somatotal1=(soma1-desconto01+acrescimo01).toFixed(2);
     ptotal1=((desconto01/soma1)*100).toFixed(2);

     if(isNaN(ptotal1)) ptotal1 = 0;

     //substitui separador decimal ponto por virgula
     soma1=soma1.replace(".", ",");
     somatotal1=somatotal1.replace(".", ",");
     //a regex abaixo coloca um ponto a esquerda de cada grupo de 3 digitos desde que não seja no inicio do numero
     $("#total01").val((soma1).replace(/\B(?=(\d{3})+(?!\d))/g, "."));    
     document.getElementById('p1').innerHTML =  '% ' + parseFloat(ptotal1);     
     $("#totalGeral01").val((somatotal1).replace(/\B(?=(\d{3})+(?!\d))/g, "."));     
}

inserir a descrição da imagem aqui

  • 2

    Before replacing the comma by a period, you need to remove all points before. It would be: .replace(/\./g, "").replace(",", ".")

  • Thus? var acrescimo01 = (Document.getElementById("acrescimo01"). value). replace("." , ""); acrescimo01 = (Document.getElementById("acrescimo01"). value). replace(",", "." ); acrescimo01=Number(acrescimo01);

  • I changed the comment I made. Take a look. You have to use a regex to remove all the dots. But I don’t know if this is the cause of Nan, but it is necessary to do this before calculating.

  • Our it worked out that I falter. Thank you very much, Ball Show.

  • Blz. I will post a reply for future references. Abs!

1 answer

3


You need to replace it before you remove all the points from the number, then replace it with a dot.

This is because if you just replace the comma by a dot, it will result in an invalid number when there are thousand separators. For example:

6.000,00 (seis mil) virará 6.000.00(?!)

With the two Places:

6.000,00 (seis mil) virará 6000.00 ✔

To remove all points (.) possible number, you have to use a regular expression in replace (use only .replace(".", "") will remove only the first point):

.replace(/\./g, "")

The \. represents the point (use Escaper \ because the point alone has function within the regular expression), and the g (of global) to catch all occurrences.

Then another simple replace just to replace the comma:

.replace(",", ".")

Joining the two, in the order from left to right, stands:

.replace(/\./g, "").replace(",", ".")

Browser other questions tagged

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