Calculate subtotal even if all fields are not completed

Asked

Viewed 57 times

1

I have a form where procedures values need to be entered to be calculated at the end, but the values will not always be typed in all fields. I wanted to know what is the validation for the total procedure exit even if values are entered only in two fields for example. Also, in the calculation is not leaving what has after the comma. For example: if I put 12,50 + 12,50 only comes out 24 and not 25,00

  • You can put here an example of the HTML you have and the code you are using to calculate?

  • Your question is very broad since among the fields there can be mathematical operations of sum, subtraction, multiplication, division, percentage. Better do what Sergio asked.

  • When some field is not filled in some problem occurs?

  • Welcome Anne, this post will be very useful for your next questions https://answall.com/help/mcve

  • Also https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions/

  • Be sure to mark an answer as accepted if you have solved your difficulty. See how in https://i.stack.Imgur.com/evLUR.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Show 1 more comment

1 answer

1

In a general way I made a routine that can be easily adapted to any type of operation and amount of fields.

Just add the fields, retrieve the values and do the proper calculations on the variable total

  //formata o resultado para reais e decimais com virgula
  function reais(n) {
    var l = Math.floor(n);
    var r = Math.round((100*n)%100);
    if (r<10) return "R$"+l+",0"+r;
    if (r==100) return "R$"+(l+1)+",00";
    return "R$"+l+"."+r;
  }

  function calculate(f) {
    //retorno dos valores dos campos
    var id1=document.getElementById('id1').value;
    var id2=document.getElementById('id2').value;
    var id3=Number(document.getElementById('id3').value);
    var id4=document.getElementById('id4').value;
    var id5=document.getElementById('id5').value;
    //para não invalidar o resultado caso haja alguma divisão no total
    if(id5==""){
       id5=1;
    }
    //aqui as operações envolvendo os campos
    var total=(((id1*id2)+id3)-id4)/id5;
    f.total.value=reais(total);

  }
<form>
  Quantidade 
  <input type="text" name="input1"t id="id1">
  Preço Unitário
  <input type="text" name="input2" id="id2"><br>
  Imposto
  <input type="text" name="input3" id="id3"><br>
  Desconto
  <input type="text" name="input4" id="id4"><br>
  Parcelas
  <input type="text" name="input5" id="id5">

  <p>
  <table>
    <tr><td>
      Resultado:</td><td><input type=text name=total size=8>
    </td></tr>
  </table>
  <p>
  <input type=button value=Calculate onClick="calculate(this.form);"> 
</form>

The operator + may surprise you, depending on how you use it.

  • Everyone knows that the operator + serves at least two things: adding numbers and concatenating strings!
  • In the example of the variable response var id3 has been transformed into a number with the function Number to perform the addition operation because otherwise there would be a concatenation.

Instead of using the function Number in the variable var id3 we could use the operator himself + to convert it into number.

See how:

In place of:

var id3=Number(document.getElementById('id3').value);

Use

var id3=document.getElementById('id3').value;

And replace the var total for

var total=(((id1*id2)+(+id3))-id4)/id5;

Example

//operador + para conveeter em numero
console.log(10 + +'10');
//sem o operador +
console.log(10 + '10');

Browser other questions tagged

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