ccocker
ccocker

Reputation: 1236

Intl.NumberFormat: Locales and Symbols

I am trying to use the Intl.NumberFormat to change display of my currency in Angular6 Application. It works fine with USD, however when i try to use it for South African Rand or United Arab Emirates Dirham i keep getting an:

RangeError: Value R out of range for numberformat options property currencyDisplay

I cannot seem to find anywhere that lists all the valid locales and symbols that can be used so wondering if the problem is those currencies are not supported.

Can anyone advise if they are supported or where i can find the valid list to check against.

Sample code below, the currency ZAR works but the currencyDisplay fails with out of range error:

const currencyFormat = new Intl.NumberFormat('af', {
            style: 'currency',
            currency: 'ZAR',
            currencyDisplay: 'R',
            minimumFractionDigits: 2
        });

Upvotes: 3

Views: 4095

Answers (3)

Gouranga Satapathy
Gouranga Satapathy

Reputation: 432

const number = 1.5;


console.log(new Intl.NumberFormat('en-za', { style: 'currency', currency: 'ZAR' }).format(number));

OUTPUT -

R 1,50

Upvotes: 1

user3856491
user3856491

Reputation: 61

Much later but may help others. This worked for me as it yields a value prefixed with the 'R' as used in South Africa.

let displayValue = new Intl.NumberFormat('en-ZA', {
  style: 'currency',
  currency: 'ZAR',
  minimumFractionDigits: 2
}).format(this.displaySubTotal)

For correct locale - Refer to: https://www.localeplanet.com/icu/en-ZA/index.html

Upvotes: 4

CanIHazCookieNow
CanIHazCookieNow

Reputation: 152

currencyDisplay only take name or symbol

new Intl.NumberFormat('af', {
            style: 'currency',
            currency: 'ZAR',
            currencyDisplay: 'name',
            minimumFractionDigits: 2
        }).format(1)
// "1.00 South African rand"

new Intl.NumberFormat('af', { style: 'currency', currency: 'ZAR', currencyDisplay: 'symbol', minimumFractionDigits: 2 }).format(1) \ "ZAR 1.00"


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

Upvotes: 0

Related Questions