Format Float value as currency using Jquery

5

I have a function that does the sum of values of a line, I would like to know if it is possible to put the value as currency like this:

R$ 16,00

You're leaving this way

R$ 16.00

But I would not want to use third-party library.

My code:

var somaLinha = 0;
$('.columnRight  > label').slice(-5).each(function( i, coluna ) {
        var valorColuna = $(coluna).text().replace('R$', '').replace(',', '.');
        valorColuna = parseFloat(valorColuna);
        somaLinha += (!isNaN(valorColuna) ? valorColuna : 0);
});
if (somaLinha != 0) {
    $('.columnRight > label').last().text('R$ ' + parseFloat(somaLinha, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,")).toString();
}
    
asked by anonymous 06.12.2015 / 18:43

5 answers

7

Well I figured it out, so follow the answer.

function mascaraValor(valor) {
    valor = valor.toString().replace(/\D/g,"");
    valor = valor.toString().replace(/(\d)(\d{8})$/,"$1.$2");
    valor = valor.toString().replace(/(\d)(\d{5})$/,"$1.$2");
    valor = valor.toString().replace(/(\d)(\d{2})$/,"$1,$2");
    return valor                    
}

The parameter I pass is a float value, and I pass it this way:

mascaraValor(valor.toFixed(2))
    
08.12.2015 / 10:56
7

You can do so, which also works:

function currencyFormatted(value, str_cifrao) {
    return str_cifrao + ' ' + value.formatMoney(2, ',', '.');
}

Number.prototype.formatMoney = function (c, d, t) {
    var n = this,
        c = isNaN(c = Math.abs(c)) ? 2 : c,
        d = d == undefined ? "." : d,
        t = t == undefined ? "," : t,
        s = n < 0 ? "-" : "",
        i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

currencyFormatted(16.00, 'R$');
    
08.12.2015 / 13:24
5

I know you asked for it in JQuery, but JavaScript already has an internal function for it. Number.prototype.toLocaleString

var valor = 16.00;
var texto = valor.toLocaleString("pt-BR", { style: "currency" , currency:"BRL"});

console.log(texto);
    
08.12.2015 / 11:28
1

just put .replace('.',','); on the output, worked here with me

    
06.04.2017 / 17:05
0

In my case the system returned the type "money" in the database and the C # language returned to view the type "decimal". After many tests I was able to return correctly formatted values using the following function:

function formatReal(numero) {
    var tmp = numero + '';
    var neg = false;

    if (tmp - (Math.round(numero)) == 0) {
        tmp = tmp + '00';        
    }

    if (tmp.indexOf(".")) {
        tmp = tmp.replace(".", "");
    }

    if (tmp.indexOf("-") == 0) {
        neg = true;
        tmp = tmp.replace("-", "");
    }

    if (tmp.length == 1) tmp = "0" + tmp

    tmp = tmp.replace(/([0-9]{2})$/g, ",$1");

    if (tmp.length > 6)
        tmp = tmp.replace(/([0-9]{3}),([0-9]{2}$)/g, ".$1,$2");

    if (tmp.length > 9)
        tmp = tmp.replace(/([0-9]{3}).([0-9]{3}),([0-9]{2}$)/g, ".$1.$2,$3");

    if (tmp.length = 12)
        tmp = tmp.replace(/([0-9]{3}).([0-9]{3}).([0-9]{3}),([0-9]{2}$)/g, ".$1.$2.$3,$4");

    if (tmp.length > 12)
        tmp = tmp.replace(/([0-9]{3}).([0-9]{3}).([0-9]{3}).([0-9]{3}),([0-9]{2}$)/g, ".$1.$2.$3.$4,$5");

    if (tmp.indexOf(".") == 0) tmp = tmp.replace(".", "");
    if (tmp.indexOf(",") == 0) tmp = tmp.replace(",", "0,");

    return (neg ? '-' + tmp : tmp);
}

Tested up with negative numbers and returned correctly.

    
18.04.2017 / 17:46