Formatting/Punctuation of real values

Asked

Viewed 47 times

1

I have a value comparison to check if a withdrawal is possible or not.

I’m having trouble formatting the values and making this comparison.

Behold:

inserir a descrição da imagem aqui

And on the console this bringing me formatted this way:

inserir a descrição da imagem aqui

That is, whenever it is valid if the amount to withdraw is less than the available balance, it enters the condition that returns that the value for the serve is greater than the available value, because of the formatting/score.

Follows code:

 if (parseFloat($("#txtVlrSacar").val().replace(",", ".")) > parseFloat($("#lblSaldo").text().replace(",", "."))) {
                        LimparSaque();
                        swal("", "Valor para saque maior do que valor disponível!", "warning");
                        return;
                    }
  • parseFloat($("#lblSaldo").text() would not be parseFloat($("#lblSaldo").val()

  • for some reason keeps returning with Nan if I change to val()

  • 1

    Try replacing the dot with blank and comma with dot $("#lblSaldo").text().replace('.","").replace(",", ".")

2 answers

1

Igor, you first need to replace the "." point with nothing. First you must format the thousand, then format the decimal.

var saldo = parseFloat($("#lblSaldo").text().replace('.","").replace(",", ".");
var valorSacar = parseFloat($("#txtVlrSacar").text().replace('.","").replace(",", ".");

if (valorSacar > saldo) {
    LimparSaque();
    swal("", "Valor para saque maior do que valor disponível!", "warning");
    return;
}

1


I gave an organized to facilitate understanding and improve maintenance.

Follows the code:

var txtVlrSacar = $("#txtVlrSacar").val();
var txtSaldo = $("#lblSaldo").text();

//Retira todas as ocorrências de pontos. Em seguida transforma as vírgulas existentes em ponto para que seja possível a conversão para float.
var txtVlrSacarFormatado = txtVlrSacar.replace(/\./g, '').replace(",",".");
var txtSaldoFormatado = txtSaldo.replace(/\./g, '').replace(",",".");

var numVlrSacar = parseFloat(txtVlrSacarFormatado);
var numSaldo = parseFloat(txtSaldoFormatado);

if (numVlrSacar > numSaldo) {
    LimparSaque();
    swal("", "Valor para saque maior do que valor disponível!", "warning");
    return;
}

Browser other questions tagged

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