Problem with Mascara Currency and calculation

1

I have a function that is called here:

 $("#valorPrimeiroPedido").focusout(function () {
        var valor = $("#valorPrimeiroPedido").val();
        calcularValorMercadoria(valor);      
    });

$("#valorPrimeiroPedido").val() already brings me the formatted value (ex: R $: 1,500,56) . When it loses focus it calls the function below, passing the formatted value. There is the error. I can not calculate the IPI and display in HTML .

 function calcularValorMercadoria(valor) {

            var _valorPedido = CurrencyFormat(valor);
            var _valorPedidoComIPI = _valorPedido * 1.1;

            console.log(_valorPedido);
            $("#ipi").html("Valor do pedido com IPI: R$ " + CurrencyFormat(_valorPedidoComIPI));
        }
Erro: Uncaught ReferenceError: *CurrencyFormat is not defined*.

When I take the mask CurrencyFormat , the value returned is: R$ NaN

I'm using some libraries:

<script src="~/Content/modalAjax/jquery-3.3.1.min.js"></script>
<script src="~/Content/modalAjax/bootstrap.min.js"></script>
<script src="~/Scripts/jquery.maskedinput.js"></script>
<script src="~/Areas/Representantes/Script/cliente/cadastro.js"></script>
<script src="~/Areas/Representantes/Script/cliente/abas.js"></script>
<link href="~/Areas/Representantes/Script/cliente/ClienteEstilo.css" rel="stylesheet" />
<script src="~/Content/modalAjax/bootstrapcdn.min.js"></script>
<script src="~/Content/modalAjax/ajaxmodaldeconfirmacaobootbox.min.js"></script>
<script src="https://cdn.rawgit.com/plentz/jquery-maskmoney/master/dist/jquery.maskMoney.min.js"></script>

Summary: I need to get the value in the Real format, calculate the IPI and also show the calculation in the Real format and then save in the float format (in SQL).

    
asked by anonymous 05.04.2018 / 16:30

1 answer

3

It is necessary to remove some characters as letters and commas in order to perform calculations in JavaScript .

Since you are using the jquery.maskMoney.min.js library, just use the unmask event. This event will return the values without the above characters, for example:

'R$ 1.234,56' => 1234.56

In this way, JavaScript will be able to convert to a numeric value and thus you can perform the calculation.

Commented example:

/* Aplica a máscara no campo */
$(function() {
  $('#valorPrimeiroPedido').maskMoney({
    prefix: 'R$ ',
    thousands: '.',
    decimal: ','
  });
})

/* Calcula o valor da mercadoria */
$("#valorPrimeiroPedido").focusout(function() {

  /* Captura o valor sem máscara (sem o R$) */
  let valor = $('#valorPrimeiroPedido').maskMoney("unmasked")[0];

  calcularValorMercadoria(valor);
});

/* Função para calcular o valor da mercadoria */
function calcularValorMercadoria(_valorPedido) {
  let _valorPedidoComIPI = _valorPedido * 1.1;

  console.log(_valorPedido);
  $("#ipi").html("Valor do pedido com IPI: R$ " + CurrencyFormat(_valorPedidoComIPI));
}

/* Função para retornar os valores com "máscara" */
function CurrencyFormat(value) {
  return new Number(value).toLocaleString("ptb",{
    style: "currency",
    currency: "BRL"
  })
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdn.rawgit.com/plentz/jquery-maskmoney/master/dist/jquery.maskMoney.min.js"></script>

<input type="text" id="valorPrimeiroPedido" />

<span id="ipi"></span>

About the error below

Uncaught ReferenceError: CurrencyFormat is not defined.

The following error occurs because the CurrencyFormat function was not created.

    
05.04.2018 / 17:24