Problem with Mascara Currency and I calculate

Asked

Viewed 1,572 times

0

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 cannot calculate the IPI and show 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 off 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 take the value in Real format, calculate the IPI and show the calculation also in Real format and then save in format float (in SQL).

  • You have to use the replace to remove the R$ and replace the comma with the point.

1 answer

2


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

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

'R$ 1.234,56' => 1234.56

Thus, the Javascript you will be able to convert to a numerical value and you will be able to 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>
<script src="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 error below happens because the function CurrencyFormat was not created.

  • Thank you very much, that’s right. It worked.

  • strange, I made the same code changing variable name and appears here: Typeerror: numer01.maskMoney is not a Function

Browser other questions tagged

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