Skip to content
Advertisement

Intl.NumberFormat doesn’t convert to pt-BR locale

I have this sample of code:

const formCurrency = new Intl.NumberFormat('pt-BR', {
    style: 'currency',
    currency: 'BRL',
    minimumFractionDigits: 2
})

if the input is:

var money = 1000.50

formCurrency.format(money)

the expected output is: R$ 1.000,50, but instead it gives: R$ 1,000.50

Does anyone know how to change the , with ., or other way to do this using Intl?

I’ve already tried changing the locale to de-DE, but doesn’t work as well. With other style the R$ changes, but the rest of the punctuation don’t.

Advertisement

Answer

Thanks to Alvaro, I’ve found a way. According to MDN and Node documentation itself, Node.js support only en-US locale. So to make it work I needed to:

  • install the full-icu package with npm i full-icu
  • do a npm-rebuild after the instalation
  • add inside the package.json file, add the code:

    "scripts": { "start":"node --icu-data-dir=node_modules\full-icu YOURAPP.js" }

  • run the node application with npm start

Now it gets the correct locale and do the correct punctuation too.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement