Formatting / Punctuation of values in Reais

1

I have a comparison of values to check whether it is possible to take a draw or not.

I'm having trouble formatting the values and comparing them.

See:

Andintheconsoleisbringingmeformattedthisway:

Thatis,wheneveritisvalidatediftheamounttobewithdrawnislessthantheavailablebalance,itentersintotheconditionthatreturnsthatthevalueforthedrawisgreaterthantheavailablevalue,becauseoftheformatting/punctuation.

Followthecodebelow:

if(parseFloat($("#txtVlrSacar").val().replace(",", ".")) > parseFloat($("#lblSaldo").text().replace(",", "."))) {
                        LimparSaque();
                        swal("", "Valor para saque maior do que valor disponível!", "warning");
                        return;
                    }
    
asked by anonymous 19.06.2018 / 19:07

2 answers

1

I have organized one to make it easier to understand and improve maintenance.

Follow 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;
}
    
20.06.2018 / 00:21
1

Igor, you need to first replace the "." You're welcome. 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;
}
    
19.06.2018 / 20:16