How do I make sure that when I receive a numerical value in the table, it is converted into R$ currency?

Asked

Viewed 50 times

-1

Example:

  • in the Table field "valor", if she gets this 587644, she must be converted to 5.876,44.

Someone there can help me?

HTML code:

<div id="dfPessoas">
    <form name="fPessoas">
    <select class="custom-select my-1 mr-sm-2" id="formadepagamento">
                <option selected>Formas de Pagamento</option>
                <option>Elo Crédito</option> <!-- com o value informado, ele pega o valor que esta obtido no value -->
                <option>Elo Débito</option>
                <option>Alelo</option>
                <option>Visa</option>
                <option>Visa Electron</option>
                <option>Sodexo</option>
                <option>Mastercard</option>
                <option>Maestro</option>
                <!-- <option value="8">Maestro</option> // Quando remove o "value" ele pega o nome do campo -->
    </select>
            <label for="adquirente">*Adquirente</label>
            <select class='form-control form-control-sm' data-placeholder="Digite um nome p/ procurar o adquirente"
            value="adquirentes" id="adquirentes"></select>
            <label for="inputAddress">Valor R$</label>
            <input type="number" name="txtValorformadepagamento" class="form-control" id="valorformadepagamento" placeholder="Valor">
            <label for="inputAddress">Cupom</label>
            <input type="text" name="txtCupomformasdepagamento" class="form-control" id="cupomformasdepagamento" placeholder="Cupom">
    </form>
</div><br>

<div class="container">
    <div class="row">
        <div class="col-md-6 text-left">     
        <button type="button"  class="btn btn-success"  onclick="cadLancamentos(adquirentes, formadepagamento.value, valorformadepagamento.value, cupomformasdepagamento.value)" />Adicionar</button>
        <button type="button" class="btn btn-warning">Limpar</button>
        </div>    
    </div>
</div>
<hr>

<div>
    <table class="table table-dark" id="dtAdquirente">
        <thead>
        <tr>
            <th scope="col">Codigo:</th>
            <th scope="col">Adquirente:</th>
            <th scope="col">Forma de Pagamento:</th>
            <th scope="col">Valor</th>
            <th scope="col">Cupom</th>
        </tr>
    </thead>
    </table>
</div>

Javascript code:

function cadLancamentos(adquirente, formadepagamento, valorformadepagamento, cupomformasdepagamento) {

    var tb = document.getElementById("dtAdquirente");
    var qtdLinhas = tb.rows.length;
    var linha = tb.insertRow(qtdLinhas);

    var cellCodigo = linha.insertCell(0);
    var cellAdquirente = linha.insertCell(1);
    var cellFormadepagamento = linha.insertCell(2);
    var cellValorformadepagamento = linha.insertCell(3).mask("000.000,000,000");
    var cellCupomformasdepagamento = linha.insertCell(4);

    cellCodigo.innerHTML = qtdLinhas;
    cellAdquirente.innerHTML = ($(adquirente).find('option:selected').text());
    cellFormadepagamento.innerHTML = formadepagamento;
    cellValorformadepagamento.innerHTML = valorformadepagamento;
    cellCupomformasdepagamento.innerHTML = cupomformasdepagamento;
}

2 answers

0

Solution to this doubt! I thank the help "Cmte Cardinal"...

Javascript

function moeda(a, e, r, t) {
let n = ""
  , h = j = 0
  , u = tamanho2 = 0
  , l = ajd2 = ""
  , o = window.Event ? t.which : t.keyCode;
if (13 == o || 8 == o)
    return !0;
if (n = String.fromCharCode(o),
-1 == "0123456789".indexOf(n))
    return !1;
for (u = a.value.length,
h = 0; h < u && ("0" == a.value.charAt(h) || a.value.charAt(h) == r); h++)
    ;
for (l = ""; h < u; h++)
    -1 != "0123456789".indexOf(a.value.charAt(h)) && (l += a.value.charAt(h));
if (l += n,
0 == (u = l.length) && (a.value = ""),
1 == u && (a.value = "0" + r + "0" + l),
2 == u && (a.value = "0" + r + l),
u > 2) {
    for (ajd2 = "",
    j = 0,
    h = u - 3; h >= 0; h--)
        3 == j && (ajd2 += e,
        j = 0),
        ajd2 += l.charAt(h),
        j++;
    for (a.value = "",
    tamanho2 = ajd2.length,
    h = tamanho2 - 1; h >= 0; h--)
        a.value += ajd2.charAt(h);
    a.value += r + l.substr(u - 2, u)
}
return !1}

HTML

<label for="inputAddress">*Valor R$</label>
        <input type="text" name="valor" id="valorformadepagamento" placeholder="Digite aqui" class="form-control" onKeyPress="return(moeda(this,'.',',',event))">  

0

you can display any number float or integer using the javascript library

consider the example below:

const val = 32.93
const real = Intl.NumberFormat('pt-br', {style: 'currency', currency: 'BRL'}).format(val)

console.log(real)

Browser other questions tagged

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