Skip to content
Permalink
Browse files
[JSC] Intl.NumberFormat ignores maximumFractionDigits with compact no…
…tation (both currency and decimal)

https://bugs.webkit.org/show_bug.cgi?id=246646
rdar://101298045

Reviewed by Justin Michaud.

This patch aligns our hasFd / hasSd / needFd / needSd computation to the latest Intl.NumberFormat v3[1].
In particular, we strictly align our implementation to [2] function, and this change fixes compact notation's fd handling.

[1]: https://github.com/tc39/proposal-intl-numberformat-v3
[2]: https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/diff.html#sec-setnfdigitoptions

* JSTests/stress/intl-numberformat-fd-handling-v2.js: Added.
(shouldBe):
(shouldBe.fmt.resolvedOptions):
* Source/JavaScriptCore/runtime/IntlNumberFormatInlines.h:
(JSC::setNumberFormatDigitOptions):

Canonical link: https://commits.webkit.org/255691@main
  • Loading branch information
Constellation committed Oct 18, 2022
1 parent ffbe8fc commit 9c113c3fb2ab451254444ee83739c69a1bca8948
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
@@ -0,0 +1,13 @@
function shouldBe(actual, expected) {
if (actual !== expected)
throw new Error('bad value: ' + actual);
}

var fmt = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD', notation: 'compact', maximumFractionDigits: 2, compactDisplay: 'long'});

shouldBe(JSON.stringify(fmt.resolvedOptions()), `{"locale":"en-US","numberingSystem":"latn","style":"currency","currency":"USD","currencyDisplay":"symbol","currencySign":"standard","minimumIntegerDigits":1,"minimumFractionDigits":2,"maximumFractionDigits":2,"useGrouping":"min2","notation":"compact","compactDisplay":"long","signDisplay":"auto","roundingMode":"halfExpand","roundingIncrement":1,"trailingZeroDisplay":"auto","roundingPriority":"auto"}`);
shouldBe(fmt.format(97896), `$97.90K`);

var fmt = new Intl.NumberFormat('en-US', {style: 'decimal', notation: 'compact', maximumFractionDigits: 2, compactDisplay: 'long'})
shouldBe(JSON.stringify(fmt.resolvedOptions()), `{"locale":"en-US","numberingSystem":"latn","style":"decimal","minimumIntegerDigits":1,"minimumFractionDigits":0,"maximumFractionDigits":2,"useGrouping":"min2","notation":"compact","compactDisplay":"long","signDisplay":"auto","roundingMode":"halfExpand","roundingIncrement":1,"trailingZeroDisplay":"auto","roundingPriority":"auto"}`);
shouldBe(fmt.format(97896), `97.9 thousand`);
@@ -67,8 +67,15 @@ void setNumberFormatDigitOptions(JSGlobalObject* globalObject, IntlType* intlIns

bool hasSd = !minimumSignificantDigitsValue.isUndefined() || !maximumSignificantDigitsValue.isUndefined();
bool hasFd = !minimumFractionDigitsValue.isUndefined() || !maximumFractionDigitsValue.isUndefined();
bool needSd = hasSd || roundingPriority != IntlRoundingPriority::Auto;
bool needFd = (!hasSd && notation != IntlNotation::Compact) || roundingPriority != IntlRoundingPriority::Auto;

bool needSd = true;
bool needFd = true;

if (roundingPriority == IntlRoundingPriority::Auto) {
needSd = hasSd;
if (hasSd || (!hasFd && notation == IntlNotation::Compact))
needFd = false;
}

if (needSd) {
if (hasSd) {

0 comments on commit 9c113c3

Please sign in to comment.