Skip to content

Commit

Permalink
fix(@formatjs/intl-numberformat): better handling of useGrouping, fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Dec 9, 2024
1 parent f6e67ad commit 7743782
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/ecma402-abstract/NumberFormat/format_to_parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default function formatToParts(
exponent,
numberingSystem,
// If compact number pattern exists, do not insert group separators.
!compactNumberPattern && Boolean(options.useGrouping),
!compactNumberPattern && (options.useGrouping ?? true),
decimalNumberPattern,
style,
options.roundingIncrement,
Expand Down Expand Up @@ -377,7 +377,7 @@ function partitionNumberIntoParts(
notation: NumberFormatOptionsNotation,
exponent: number,
numberingSystem: string,
useGrouping: boolean,
useGrouping: UseGroupingType,
/**
* This is the decimal number pattern without signs or symbols.
* It is used to infer the group size when `useGrouping` is true.
Expand Down Expand Up @@ -423,10 +423,15 @@ function partitionNumberIntoParts(
// unless the rounded number is greater than 10000:
// NumberFormat('de', {notation: 'compact', compactDisplay: 'short'}).format(1234) //=> "1234"
// NumberFormat('de').format(1234) //=> "1.234"
if (
useGrouping &&
(notation !== 'compact' || x.greaterThanOrEqualTo(10000))
) {
let shouldUseGrouping = false
if (useGrouping === 'always') {
shouldUseGrouping = true
} else if (useGrouping === 'min2') {
shouldUseGrouping = x.greaterThanOrEqualTo(10000)
} else if (useGrouping === 'auto' || useGrouping) {
shouldUseGrouping = notation !== 'compact' || x.greaterThanOrEqualTo(10000)
}
if (shouldUseGrouping) {
// a. Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator.
// For currency we should use `currencyGroup` instead of generic `group`
const groupSepSymbol =
Expand Down
8 changes: 8 additions & 0 deletions packages/intl-numberformat/tests/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,11 @@ test('#4678', () => {
})
expect(nf.format(1050)).toEqual('1K')
})

test('#4476', () => {
const formatter = new NumberFormat('zh-Hant', {
notation: 'compact',
useGrouping: 'always',
})
expect(formatter.format(1000)).toEqual('1,000')
})

0 comments on commit 7743782

Please sign in to comment.