Calculate input with formatting (mascara)

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 being able to calculate when it is in the house of thousands

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

resultado = (parseFloat($valor.replace(",", ".")) * parseInt($quantidade));
$('#Total').val(result.toFixed(2));
    
asked by anonymous 08.02.2016 / 18:22

1 answer

2

The library you use has the .maskMoney('unmasked') method to extract the value as float , which 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><scriptsrc="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"/>
    
11.02.2016 / 12:56