10

I don't understand how NumberFormat works.

In France we never use $US so why do I get the following?

new Intl.NumberFormat("fr-FR",{
            style: 'currency',
            currency: 'USD',
            minimumFractionDigits: 2,
         }).format("345")
"345,00 $US"

new Intl.NumberFormat("fr-FR",{
            style: 'currency',
            currency: 'EUR',
            minimumFractionDigits: 2,
         }).format("345")
"345,00 €"

Also: the following does not make any sense to me either. I tried random locales to see the impact and get different results for these 2:

new Intl.NumberFormat("en-HOS",{
            style: 'currency',
            currency: 'USD',
            minimumFractionDigits: 2,
         }).format("345")
"345,00 $US"

new Intl.NumberFormat("en-HOSSDDG",{
            style: 'currency',
            currency: 'USD',
            minimumFractionDigits: 2,
         }).format("345")
"$345.00"

Is this API broken or I miss something?

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • The API is designed for the world and not just for USA/Europe. There are [multiple dollar currencies](https://en.wikipedia.org/wiki/Dollar) – japrescott Sep 19 '18 at 16:39
  • 2
    sure, but in france nobody ever used `$US`, why does the api return that? Isn't it intended to be used in real life formatting operations? – Sebastien Lorber Sep 19 '18 at 17:02
  • 5
    As a Canadian, if I make a purchase in US currency but it only shows "$", I would assume it's in Canadian. - If my locale is set to "en-CA" and my purchase is in USD, it shows "US$". - If my locale is set to "en-US" and my purchase is in USD, it shows "$". – Phil Johnston Apr 19 '19 at 17:17

2 Answers2

35

You need to use the narrow option when formatting numbers in JS

"narrowSymbol" to use a narrow format symbol ("$100" rather than "US$100"),

const amount = 123444;
console.log(new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', currencyDisplay: 'narrowSymbol'}).format(amount));

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat for full details.

John
  • 29,788
  • 18
  • 89
  • 130
1

V8 JavaScript engine is using ICU library for i18N.

See there for all currency data: https://github.com/unicode-org/icu/tree/master/icu4c/source/data/curr

But for sure French people use $ and not $US when talking about US dollar.