Currency formatting in input calculation

Asked

Viewed 85 times

-1

The script idea is correct. I just couldn’t put the currency formatting in the "minimal preco_n" field (with value script). If I put real currency formatting in that input, it does not calculate.

			$(function(){
				$(".valor").maskMoney({symbol:'R$ ', showSymbol:true, thousands:'.', decimal:',', symbolStay: true});
			})	
      
			$(function() {
				$('#preco_minimo_n').on('input', function() {
					Number.prototype.formataReal = function(c, d, t) {
						var n = this,
							c = isNaN(c = Math.abs(c)) ? 2 : c,
							d = d == undefined ? "," : d,
							t = t == undefined ? "." : t,
							s = n < 0 ? "-" : "",
							i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
							j = (j = i.length) > 3 ? j % 3 : 0;
						return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
					};
					
					var $preco_minimo = $(this);
					var $preco_minimo_b     = $("#preco_minimo_n").val(); 
					var $preco_minimo_c   = parseFloat($preco_minimo_b.replace(/[^\d.,]/g, "").replace('.', '').replace(',', '.'))
		
					var $table = $preco_minimo.closest('div');
					
					//var $preco_estimado = $table.find("#preco_estimado_n");
					var $preco_estimado_b   = $("#preco_estimado_n").val();
					var $preco_estimado_c = parseFloat($preco_estimado_b.replace(/[^\d.,]/g, "").replace('.', '').replace(',', '.'))					
					
					var $percentual_desconto = $table.find("#percentual_desconto_n");

					//var preco_estimado = parseFloat((1 / 10).toFixed(1));
					//$preco_estimado.val(preco_estimado);
					//$percentual_desconto.val(parseFloat($preco_minimo.val()) + preco_estimado);
					$percentual_desconto.val(parseFloat((($preco_minimo_c / $preco_estimado_c) - 1) * 100).formataReal(2, ',', '.') + ' %');
				});
			});
<script src="http://www.rpgigasolucoes.com.br/beta/mmlicitacoes/js/jquery.js"></script>
<script src="http://www.rpgigasolucoes.com.br/beta/mmlicitacoes/js/jquery.maskMoney.js"></script>


<div>


			<p>
				<b>Preço mínimo</b>
				<input type="text" name="preco_minimo_n" id="preco_minimo_n" class="amount ">
			</p>
          
			<p>
				<b>Preço estimado</b>
				<input name="preco_estimado_n" id="preco_estimado_n" type="text" class="tax valor" value="R$ 590.000,00" readonly="readonly">
			</p>
       
			<p>
				<b>Percentual de desconto</b>
				<input name="percentual_desconto_n" id="percentual_desconto_n" type="text" class="total valor" readonly="readonly">
			</p>
        </div>

  • have tried to withdraw the formation to calculate?

  • $(".value") did not see this class in your HTML

1 answer

1


I couldn’t find the error in your code but I figured out a way to make it work

Script

$(function(){
   $(".amount").maskMoney({symbol:'R$ ', showSymbol:true, thousands:'.', decimal:',', symbolStay: true});
})  

$( document ).ready(function() {

Number.prototype.formataReal = function(c, d, t) {
    var n = this,
    c = isNaN(c = Math.abs(c)) ? 2 : c,
    d = d == undefined ? "," : d,
    t = t == undefined ? "." : t,
    s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
    j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

var preco_minimo = $("#preco_minimo_n");
preco_minimo.keyup( function(){

var $preco_estimado_b   = $("#preco_estimado_n").val();
var $preco_estimado_c = parseFloat($preco_estimado_b.replace(/[^\d.,]/g, "").replace('.', '').replace(',', '.'))                    
                    
$preco_minimo_b = (preco_minimo.val());

var $preco_minimo_c = parseFloat($preco_minimo_b.replace(/[^\d.,]/g, "").replace('.', '').replace(',', '.'));

$("#percentual_desconto_n").val(parseFloat((($preco_minimo_c / $preco_estimado_c) - 1) * 100).formataReal(2, ',', '.') + ' %');
    
});

});

HTML

<p>
<b>Preço mínimo</b>
   <input type="text" name="preco_minimo_n" id="preco_minimo_n" class="amount">
</p>
          
<p>
<b>Preço estimado</b>
   <input name="preco_estimado_n" id="preco_estimado_n" type="text" class="tax valor" value="R$ 590.000,00" readonly="readonly">
</p>
       
<p>
<b>Percentual de desconto</b>
   <input name="percentual_desconto_n" id="percentual_desconto_n" type="text" class="total valor" readonly="readonly">
</p>
  • Correction instead of $(".valor").maskMoney({ the correct is $(".amount").maskMoney({
  • I presented the project and the client managed to find a bo that I would never find. The calculation is perfect, however, she simulated with an estimated price of R $ 1.000.000,00 and I believe that the regular expression gives some conflict, because, if I give an Alert, returns 1000. Then the math goes crazy.

Browser other questions tagged

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