Calculate formatted input (mask)

Asked

Viewed 1,073 times

1

I have 2 inputs, quantity and value.

The input value is using the plugin Jquery Maskmoney

it is configured as follows:

$('.money').maskMoney(
{
    allowNegative: true,
    thousands: '.',
    decimal: ',',
    affixesStay: false
});

What makes your formatting look like this : 1,000.00

But my calculation is not able to calculate when it is in the thousands

var $valor = $('#Valor'),
    $quantidade = $('#Quantidade'),
    resultado = 0;

resultado = (parseFloat($valor.replace(",", ".")) * parseInt($quantidade));
$('#Total').val(result.toFixed(2));

1 answer

2


The library you use has the method .maskMoney('unmasked') to extract the value as float, that you can use to calculate the total.

$(function(){
  var valor = $('#valor');
  var quantidade = $('#quantidade');
  var total = $('#total');
  var valorTotal = 0;

  $('.money').maskMoney({
    allowNegative: true,
    thousands: '.',
    decimal: ',',
    affixesStay: false
  })
  .maskMoney('mask'); // aplicar máscara imediatamente
  
  valorTotal = valor.maskMoney('unmasked')[0] * parseInt(quantidade.val());
  total.val(valorTotal.toFixed(2)).maskMoney('mask');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-maskmoney/3.0.2/jquery.maskMoney.min.js"></script>
<input type="text" id="valor" class="money" value="1000.00"/>
<input type="text" id="quantidade" value="3"/>
<input type="text" id="total" class="money"/>

Browser other questions tagged

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