15
I have the following code:
function numberParaReal(numero){
var formatado = "R$ " + numero.toFixed(2).replace(".",",");
return formatado;
}
function realParaNumber(texto){
var compativelComParseFloat = texto.replace("R$ ","");
compativelComParseFloat = compativelComParseFloat.replace(",",".");
var valor = parseFloat(compativelComParseFloat);
return valor;
}
However, it only displays values in this format: 1000.00.
I wanted to leave it at that: 1,000.00 ~ 100,000.00 ~ 10,000,000.00 and so on...
It would be something like 3 houses before the comma gets dot, I just don’t know how to do it.
Probably there should be functions ready for this type of situation, but as I am learning, it would not be ideal for me to use ready-made functions.
Cool guy, I didn’t know this method. Very good +1.
– JuniorNunes
@Juniornunes I think it’s really cool. There’s even an argument that adds R$ to the string:
(1231230123.23).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }); // => 
"R$1.231.230.123,23"
– nbkhope
It only works for situations where you have values after the comma. if the reported value, for example is 1000 the return is 1,000 and not 1,000.00 as expected.
– Jhonatan Mota