One option is the method Number.toLocaleString
:
function formatarValor(valor) {
return valor.toLocaleString('pt-BR');
}
console.log(formatarValor(42000));
console.log(formatarValor(150000));
console.log(formatarValor(1000000));
Using the locale pt-BR
, the numbers will be formatted as you want, with .
, if you want to use ,
, use another locale, for example: en-US
.
If you need to display the fractional part, use the parameter minimumFractionDigits
followed by the minimum number of digits:
function formatarValor(valor) {
return valor.toLocaleString('pt-BR', { minimumFractionDigits: 2 } );
}
You want a separator. It’s not to turn an entire number into real, right?
– Jéf Bueno
uses the
.toLocaleString()
– guijob
That @jbueno, a tab...
– Alan PS
You will need to present this figure as
string
. A whole will not allow you to do this.– Jéf Bueno