javascript – toLocaleString R$ Brazilian

Question:

I have a simple question about toLocaleString , I didn't know this prototype and I went to test it instead of doing the good old split and replace

var a = 10000.50
var b = a.toLocaleString('pt-BR')
console.log(b)

The output of this code should be 10.000,50 , not 10.000,5 . How does he not ignore the cents? Because that's 50 cents, not 5 .

Thanks!

Answer:

This is configurable with minimumFractionDigits and maximumFractionDigits . If you want to have the decimal number fixed then give both the same number. These values ​​can range from 0 to 20.

var a = 10000.50
var b = a.toLocaleString('pt-BR', {
  minimumFractionDigits: 3,
  maximumFractionDigits: 3
})
console.log(b); // 10.000,500
Scroll to Top