Intl.NumberFormat currency: $US currency symbol?
Asked Answered
H

2

12

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?

Heterozygote answered 19/9, 2018 at 16:28 Comment(4)
The API is designed for the world and not just for USA/Europe. There are multiple dollar currenciesElectroscope
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?Heterozygote
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 "$".Selfseeker
The first param allows you to set the format of the , and . characters, which should remain constant regardless of the currency you're dealing with, because you're French. But while browsing in France you may still use USD to pay for something online, check a US bank account, or exchange from euros, which is where the currency param comes in, adding a prefix or suffix of $US to the number but keeping the . and , formatting.Redskin
A
40

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.

Albigenses answered 23/9, 2020 at 8:29 Comment(1)
Yes you will need a polyfill such as formatjs.ioAlbigenses
H
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.

Homesteader answered 1/10, 2019 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.