ecma402 icon indicating copy to clipboard operation
ecma402 copied to clipboard

Intl.NumberFormat compact notation leaves out comma

Open maxsultan-aumni opened this issue 2 years ago • 7 comments

This only happens for numbers in the trillions in compact mode as other numbers increase the letter

const format = (val) =>
  new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    notation: "compact",
    useGrouping: true,
  }).format(val);

console.log(format(1111111222333444)); // $1111T

if you add a single digit to that number, then it adds the comma as expected. but with 4 digits in compact mode there is no comma

const format = (val) =>
  new Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
    notation: "compact",
    useGrouping: true,
  }).format(val);

console.log(format(11111111222333444)); // $11,111T

maxsultan-aumni avatar Jan 26 '23 17:01 maxsultan-aumni

This issue is not newly introduced in Intl.NumberFormat v3, so I'm moving this to the main repo.

The spec states the following:

b. Let groups be a List whose elements are, in left to right order, the substrings defined by ILND set of locations within the integer.

NFv3 adds

which may depend on the value of numberFormat.[[UseGrouping]].

So if we desire to make the current behavior conformant to the spec, this could be done by changing that line to make clear that it could also depend on the Notation setting. However, the status quo is probably fine the way it is.

sffc avatar Jan 28 '23 03:01 sffc

Thoughts @gibson042?

sffc avatar Jan 28 '23 03:01 sffc

I don't want to make that behavior nonconformant. Updating the step to clarify possible dependence upon configuration works for me.

gibson042 avatar Jan 28 '23 09:01 gibson042

So if I understand correct you're saying we could get this formatted as '$1,111T' based on a different configuration object? Can you show me what that would look like? It's not clear to me from reading the spec

maxsultan-aumni avatar Jan 30 '23 21:01 maxsultan-aumni

There are two questions to answer:

  1. What is the correct behavior?
  2. Which behaviors should ECMA-402 allow?

I'm currently suggesting that the spec should allow the currently observed behavior, regardless of what the correct behavior might be.

However, to revisit the first question, then UTS 35 spec states:

N is divided by 1000 (obtained from 10000 after removing "00" and restoring one "0"). The result is formatted according to the normal decimal pattern. With no fractional digits, that yields "12 K".

https://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats

which suggests that "$1,111T" is the expected behavior; we shouldn't lose the grouping separator. That would be a bug in ICU, not in ECMA-402.

I filed a ticket: https://unicode-org.atlassian.net/browse/ICU-22254

sffc avatar Jan 30 '23 22:01 sffc

@sffc I see, thank you for the clarification 🙏

maxsultan-aumni avatar Jan 30 '23 22:01 maxsultan-aumni

Also, note that in the latest Chrome release, you are getting your desired behavior due to the NumberFormat v3 proposal changing the semantics of useGrouping: true to be more strict.

sffc avatar Jan 30 '23 22:01 sffc

Discussed in 2024-03-28 TG2 meeting. We're having trouble reproducing the undesired behavior, and would note that it is allowed by the spec regardless. So I will close this issue.

sffc avatar Mar 28 '24 18:03 sffc

TG2 notes: https://github.com/tc39/ecma402/blob/main/meetings/notes-2024-03-28.md#intlnumberformat-compact-notation-leaves-out-comma-751

sffc avatar Mar 30 '24 06:03 sffc