Search code examples
javascriptnumber-formattingcurrency-formatting

Formatting currencies shows name of currency instead of symbol


I am using Number.prototype.toLocaleString() like this

5000.70.toLocaleString('en-AU', {
    style: 'currency',
    currency: 'EUR',
    currencyDisplay: 'symbol',
    useGrouping: true
}) // "EUR 5,000.70"

Expected outcome is either "5,000.70 €" or "€5,000.70"

Instead the output in Chrome is "EUR 5,000.70"


Solution

  • If you read up on the Intl.NumberFormat() specification, the possible values of currencyDisplay are:

    • "symbol" to use a localized currency symbol such as €, this is the default value,
    • "narrowSymbol" to use a narrow format symbol ("$100" rather than "US$100"),
    • "code" to use the ISO currency code,
    • "name" to use a localized currency name such as "dollar",

    So, it looks like it is a matter of setting currencyDisplay to narrowSymbol to achieve what you want:

    const x = 5000.70.toLocaleString('en-AU', {
        style: 'currency',
        currency: 'EUR',
        currencyDisplay: 'narrowSymbol',
        useGrouping: true
    });
    
    console.log(x); // €5,000.70