toLocaleString Brazilian R $

3

I have a simple question about toLocaleString , I did not know this prototype and I tested 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 pennies? Because these are 50 cents, not 5 .

Thank you!

    
asked by anonymous 06.10.2017 / 20:50

1 answer

3

This is configurable with minimumFractionDigits and maximumFractionDigits . If you want to have the number of decimal places fixed then give the same number to both. 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
    
06.10.2017 / 20:53