Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intl.NumberFormat currency: $US currency symbol?

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?

like image 569
Sebastien Lorber Avatar asked Sep 19 '18 16:09

Sebastien Lorber


2 Answers

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.

like image 68
John Avatar answered Oct 12 '22 23:10

John


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.

like image 37
sebastien.prudhomme Avatar answered Oct 13 '22 01:10

sebastien.prudhomme