Skip to content

Commit

Permalink
fix: specify currency in locale identifier when formatting currency p…
Browse files Browse the repository at this point in the history
…lural (#919)

Summary:
Intl.NumberFormat does not respect the currency input when currencyDisplay is "name". Without currencyDisplay, currency is correctly taken into account.

Resolves #914

Pull Request resolved: #919

Test Plan:
Build Hermes and installed on RN application, tested against multiple currency/locale pairs.

```
new Intl.NumberFormat('de-DE', {
  style: 'currency',
  currency,
  currencyDisplay: 'name',
}).format(2)
```

Reviewed By: mhorowitz

Differential Revision: D43437187

Pulled By: neildhar

fbshipit-source-id: 47a7b322fbb498e431b7db9130da3a852c5de873
  • Loading branch information
Asher Lim authored and Riccardo Cipolleschi committed Mar 7, 2023
1 parent 6146eb3 commit 21f15c5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/Platform/Intl/PlatformIntlApple.mm
Expand Up @@ -2447,18 +2447,16 @@ uint8_t getCurrencyDigits(std::u16string_view code) {
// - compactDisplay is not supported.
// - signDisplay is not supported.
// - NSNumberFormatter has maximumIntegerDigits, which is 42 by default
auto nsLocale =
[NSLocale localeWithLocaleIdentifier:u16StringToNSString(dataLocale_)];
std::u16string nsLocaleStr = dataLocale_;
nsNumberFormatter_ = [NSNumberFormatter new];
nsNumberFormatter_.locale = nsLocale;
if (style_ == u"decimal") {
nsNumberFormatter_.numberStyle = NSNumberFormatterDecimalStyle;
if (notation_ == u"scientific") {
nsNumberFormatter_.numberStyle = NSNumberFormatterScientificStyle;
}
} else if (style_ == u"currency") {
nsNumberFormatter_.numberStyle = NSNumberFormatterCurrencyStyle;
nsNumberFormatter_.currencyCode = u16StringToNSString(*currency_);
nsLocaleStr.append(u"@currency=").append(*currency_);
if (currencyDisplay_ == u"code") {
nsNumberFormatter_.numberStyle = NSNumberFormatterCurrencyISOCodeStyle;
} else if (currencyDisplay_ == u"symbol") {
Expand Down Expand Up @@ -2486,6 +2484,9 @@ uint8_t getCurrencyDigits(std::u16string_view code) {
nsNumberFormatter_.maximumSignificantDigits = significantDigits_->maximum;
}
nsNumberFormatter_.usesGroupingSeparator = useGrouping_;
auto nsLocale =
[NSLocale localeWithLocaleIdentifier:u16StringToNSString(nsLocaleStr)];
nsNumberFormatter_.locale = nsLocale;
if (style_ == u"unit") {
nsMeasurementFormatter_ = [NSMeasurementFormatter new];
nsMeasurementFormatter_.numberFormatter = nsNumberFormatter_;
Expand Down

0 comments on commit 21f15c5

Please sign in to comment.