Add percentage

0

I'm trying to add percentage, but it's not showing up correctly.

The intention is to calculate the percentage, and then add up to the value. Example:

  

100.00 + 10% = 110.00

$(document).ready(function() {

  $("#proposta").focusout(function() {
    taxa = "10.00"; //%
    v1 = $(this).val().replace(",", "");
    v2 = (v1 * taxa) / 100;
    soma = (v2 + v1);
    $("#valorProposta").val(parseFloat(soma));

  });


  $(".moeda").maskMoney();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://plentz.github.io/jquery-maskmoney/javascripts/jquery.maskMoney.min.js"></script>


<input id="proposta" type="text" class="form-control col-md-12 moeda" placeholder="Digite o valor">

<input id="valorProposta" type="text" class="form-control col-md-12 moeda" readonly>
    
asked by anonymous 18.08.2017 / 13:40

3 answers

0

RESOLVED

$(document).ready(function() {
$(".moeda").maskMoney();
  
  $("#proposta").focusout(function() {
taxa = "10.00"; //%
v1 = $(this).val().replace(",", "");
v2 = (v1 * taxa) / 100;
soma = (parseFloat(v2) + parseFloat(v1));
$("#valorProposta").val(parseFloat(soma).toFixed(2));
	

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://plentz.github.io/jquery-maskmoney/javascripts/jquery.maskMoney.min.js"></script>


<input id="proposta" type="text" class="form-control col-md-12 moeda" placeholder="Digite o valor">

<input id="valorProposta" type="text" class="form-control col-md-12 moeda" readonly>
    
18.08.2017 / 14:29
0

Just use parseFloat() in the input, as in the example:

$(document).ready(function() {
  const taxa = 10;
 
  $("#proposta").focusout(function() {  
    let valor = parseFloat($(this).val().replace(",", ""));
    valor *= taxa + 100;
    valor /= 100;
    $("#valorProposta").val(valor.toFixed(2));
  });

  $(".moeda").maskMoney();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://plentz.github.io/jquery-maskmoney/javascripts/jquery.maskMoney.min.js"></script>


<input id="proposta" type="text" class="form-control col-md-12 moeda" placeholder="Digite o valor">
<input id="valorProposta" type="text" class="form-control col-md-12 moeda" readonly>

I made some other changes, but in the end I just needed to parseFloat($(this).val().replace(",", "")) , I think that would be enough.

    
18.08.2017 / 14:35
0
  • The solution is to set the value of v1 as the number using the native function parseInt of Javascript.
  • toFixed (num) being num the number of digits after the decimal point. It should be in the range of 0 to 20 inclusive.

$(document).ready(function() {

  $("#proposta").focusout(function() {
    taxa = "10.00"; //%
    v1 = $(this).val().replace(",", "");
    v1=parseInt(v1);
    v2 = (v1 * taxa) / 100;
    soma = (v2 + v1);
    $("#valorProposta").val(parseFloat(soma).toFixed(2));

  });


  $(".moeda").maskMoney();

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://plentz.github.io/jquery-maskmoney/javascripts/jquery.maskMoney.min.js"></script>


<input id="proposta" type="text" class="form-control col-md-12 moeda" placeholder="Digite o valor">

<input id="valorProposta" type="text" class="form-control col-md-12 moeda" readonly>
    
18.08.2017 / 14:37