6
I have a function that sums the values of a line, I would like to know if it is possible to put the value as currency like this:
R$ 16,00
It’s coming out this way
R$ 16.00
But I wouldn’t 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();
}
I’ve heard about this solution, so it seems to me that browsers can implement it differently, you know something about this ?
– Gabriel Rodrigues
It implements in the same way, only it doesn’t work in Safari and in versions of mobile browsers (except Chrome). Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString
– Danilo Pádua
As far as I know, this method uses the Internationalization API, its implementation depends on the Browser, but the result should be the same, at least I do not know cases of different results depending on the Browser, you can consult Can I Use to see that Apple doesn’t like this API.
– Tobias Mesquita