Take calculated values of two inputs and subtract for another input

Asked

Viewed 351 times

1

I have a form that has a field filled out from the bank. In this form, I have to do 2 percentage accounts, take these two sum values and finally take the start value and subtract from the sum of percentages.

I set up the fields, but the javascript I could not function. It can be with JQuery?

$("#percentual1").change(function () {
                    var valor = $("#valor").val();                     
                    var resultado1 = valor * (parseInt($(this).val()) / 100);
                    var resultado1Arrendodado = parseFloat(resultado1.toFixed(2));
                    $("#valor1").val(resultado1Arrendodado);
                    var ValorFinal1 = parseFloat($('#valor1').val());
                    var Valor1Arrendodado = parseFloat(ValorFinal1.toFixed(2));
                    var Valor2 = parseFloat(0.00);
                    var TotalSobra = Valor1Arrendodado + Valor2;
                    if (Number.isNaN(TotalSobra)) {
                        TotalSobra = 0;
                        $('#valorSobra').val(TotalSobra);
                    } else {
                        $('#valorSobra').val(TotalSobra);
                    }
                });
                $("#percentual2").change(function () {
                    var valor = $("#valor").val();
                    var resultado2 = valor * (parseInt($(this).val()) / 100);
                    var resulado2Arrendodado = parseFloat(resultado2.toFixed(2));
                    $("#valor2").val(resulado2Arrendodado);
                    var Valor1 = parseFloat($('#valor1').val());
                    var Valor1Arrendodado = parseFloat(Valor1.toFixed(2));
                    var Valor2 = parseFloat($('#valor2').val());
                    var Valor2Arrendodado = parseFloat(Valor2.toFixed(2));
                    var TotalSobra = Valor1Arrendodado + Valor2Arrendodado;
                    if (Number.isNaN(TotalSobra)) {
                        TotalSobra = 0;
                        $('#valorSobra').val(TotalSobra);
                    } else {
                        $('#valorSobra').val(TotalSobra);
                    }
                });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <div class="row">
 <div class="form-group col-md-4">
 <label for="valor">Valor (R$)</label>                               
 <input type="text" class="form-control" name="valor" id="valor"  value="69.00" readonly >
</div>
<div class="form-group col-md-4">
<label for="percentual1">1ºPercentual (%)</label>                                
<input type="text" class="form-control " name="percentual1" id="percentual1" title="Informe o percentual" placeholder="0.00 %"  >
</div>
<div class="form-group col-md-4">
<label for="valor1">Valor 1</label>                                
<input type="text" class="form-control" name="valor1" id="valor1"  placeholder="0.00" readonly >
</div>                                    
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="valor">Valor (R$)</label>                                
<input type="text" class="form-control" name="valor" id="valor"  value="69.00" placeholder="0.00" readonly >
</div>
<div class="form-group col-md-4">
<label for="percentual2">2º Percentual (%)</label>                               
<input type="text" class="form-control" name="percentual2" id="percentual2" placeholder="0.00%"  >
 </div>
 <div class="form-group col-md-4">      
 <label for="valor2">Valor 2</label>                                
 <input type="text" class="form-control" name="valor2" id="valor2"  placeholder="0.00" readonly >
 </div>                                    
 </div>

 <div class="row">
 <div class="form-group col-md-4">
 <label for="valor">Valor (R$)</label>                                
 <input type="text" class="form-control" name=valor" id="valor"  value="69.00" placeholder="0.00" readonly >
                                    </div>

<div class="form-group col-md-4">
<label for="valorSobra">Valor Sobra</label>                                
<input type="text" class="form-control " name="valorSobra" id="valorSobra" >
 </div>                                    
 </div>

  • @Sergio It’s not the same thing. Strange to say it’s duplicated!

  • Amanda, if it’s not the same problem we reopened of course. You can explain better the difference between the two?

  • @Sergio, by the example you passed, is percentage calculation. My problem would be another. I need to do the percentage count on two inputs, add these values and finally a subtraction. Example: 50 (this is the initial value) . 10% of 50 + 5% of 50 . We add these two values and I want to show the value of 50 - (percentage 1 + percentage 2) . Jesus... I’m dying to solve this. I have tried other examples and this one you passed did not work. Thank you

  • Which part you can’t fix?

  • 1

    If the given question is not enough, there’s this other one here that has pretty much all the mathematical operations you might need. http://answall.com/questions/162532/70 - If you have a specific part that you cannot resolve, please [Edit] ask the question and make it clear where the exact doubt is. And don’t forget to put your Javascript, and describe what goes wrong, otherwise there is no way to find the error.

  • @Sergio made the suggested modifications. My question is that the code works using two decimal places... but if I enter 3% in percentual1 and 2% in percentage 2... I receive a value with several houses.

  • Use $('#valorSobra'). val(Totalsobra.toFixed(2))

  • Amanda! Reopen. I will reply later also to try to clarify.

  • @Marcoviniciussoaresdalalba already did that. It’s still the same.

Show 4 more comments

1 answer

1


As said in comment . toFixed(2) adding toFixed will round the value.

Here follows the working code.

$("#percentual1").change(function () {
                    var valor = $("#valor").val();                     
                    var resultado1 = valor * (parseInt($(this).val()) / 100);
                    var resultado1Arrendodado = parseFloat(resultado1.toFixed(2));
                    $("#valor1").val(resultado1Arrendodado);
                    var ValorFinal1 = parseFloat($('#valor1').val());
                    var Valor1Arrendodado = parseFloat(ValorFinal1.toFixed(2));
                    var Valor2 = parseFloat(0.00);
                    var TotalSobra = Valor1Arrendodado + Valor2;
                    if (Number.isNaN(TotalSobra)) {
                        TotalSobra = 0;
                        $('#valorSobra').val(TotalSobra);
                    } else {
                        $('#valorSobra').val(TotalSobra);
                    }
                });
                $("#percentual2").change(function () {
                    var valor = $("#valor").val();
                    var resultado2 = valor * (parseInt($(this).val()) / 100);
                    var resulado2Arrendodado = parseFloat(resultado2.toFixed(2));
                    $("#valor2").val(resulado2Arrendodado);
                    var Valor1 = parseFloat($('#valor1').val());
                    var Valor1Arrendodado = parseFloat(Valor1.toFixed(2));
                    var Valor2 = parseFloat($('#valor2').val());
                    var Valor2Arrendodado = parseFloat(Valor2.toFixed(2));
                    var TotalSobra = Valor1Arrendodado + Valor2Arrendodado;
                    if (Number.isNaN(TotalSobra)) {
                        TotalSobra = 0;
                        $('#valorSobra').val(TotalSobra.toFixed(2));
                    } else {
                        $('#valorSobra').val(TotalSobra.toFixed(2));
                    }
                });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <div class="row">
 <div class="form-group col-md-4">
 <label for="valor">Valor (R$)</label>                               
 <input type="text" class="form-control" name="valor" id="valor"  value="69.00" readonly >
</div>
<div class="form-group col-md-4">
<label for="percentual1">1ºPercentual (%)</label>                                
<input type="text" class="form-control " name="percentual1" id="percentual1" title="Informe o percentual" placeholder="0.00 %"  >
</div>
<div class="form-group col-md-4">
<label for="valor1">Valor 1</label>                                
<input type="text" class="form-control" name="valor1" id="valor1"  placeholder="0.00" readonly >
</div>                                    
</div>
<div class="row">
<div class="form-group col-md-4">
<label for="valor">Valor (R$)</label>                                
<input type="text" class="form-control" name="valor" id="valor"  value="69.00" placeholder="0.00" readonly >
</div>
<div class="form-group col-md-4">
<label for="percentual2">2º Percentual (%)</label>                               
<input type="text" class="form-control" name="percentual2" id="percentual2" placeholder="0.00%"  >
 </div>
 <div class="form-group col-md-4">      
 <label for="valor2">Valor 2</label>                                
 <input type="text" class="form-control" name="valor2" id="valor2"  placeholder="0.00" readonly >
 </div>                                    
 </div>

 <div class="row">
 <div class="form-group col-md-4">
 <label for="valor">Valor (R$)</label>                                
 <input type="text" class="form-control" name=valor" id="valor"  value="69.00" placeholder="0.00" readonly >
                                    </div>

<div class="form-group col-md-4">
<label for="valorSobra">Valor Sobra</label>                                
<input type="text" class="form-control " name="valorSobra" id="valorSobra" >
 </div>                                    
 </div>

Browser other questions tagged

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