Skip to content

Commit d55c61a

Browse files
authored
Fixed Currency formatter bug with .grouping(.never) (swiftlang#1809)
Co-authored-by: Nick Randall <nick.randall@uber.com>
1 parent 111a94d commit d55c61a

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

Sources/FoundationInternationalization/Formatting/Number/NumberFormatStyleConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ extension CurrencyFormatStyleConfiguration.Collection {
853853
s += roundingIncrement.skeleton + " "
854854
}
855855
if let group = group {
856-
s += group.skeleton
856+
s += group.skeleton + " "
857857
}
858858
if let signDisplayStrategy = signDisplayStrategy {
859859
s += signDisplayStrategy.skeleton + " "

Tests/FoundationInternationalizationTests/Formatting/NumberFormatStyleICUSkeletonTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ private struct NumberFormatStyleICUSkeletonTests {
8787
#expect(isoCodeFormatter.skeleton == "currency/USD unit-width-iso-code sign-never")
8888
}
8989

90+
@Test func currencySkeleton_groupingWithRounding() throws {
91+
let style: IntegerFormatStyle<Int>.Currency = .init(code: "USD", locale: Locale(identifier: "en_US"))
92+
93+
let groupedFormatter = ICUCurrencyNumberFormatter.create(for: style.grouping(.never))!
94+
#expect(groupedFormatter.skeleton == "currency/USD unit-width-short group-off")
95+
96+
let roundedFormatter = ICUCurrencyNumberFormatter.create(for: style.rounded(rule: .toNearestOrEven))!
97+
#expect(roundedFormatter.skeleton == "currency/USD unit-width-short rounding-mode-half-even")
98+
99+
let combinedFormatter = ICUCurrencyNumberFormatter.create(for: style.grouping(.never).rounded(rule: .toNearestOrEven))!
100+
#expect(combinedFormatter.skeleton == "currency/USD unit-width-short group-off rounding-mode-half-even")
101+
102+
let reversedFormatter = ICUCurrencyNumberFormatter.create(for: style.rounded(rule: .toNearestOrEven).grouping(.never))!
103+
#expect(reversedFormatter.skeleton == "currency/USD unit-width-short group-off rounding-mode-half-even")
104+
}
105+
90106
@Test func styleSkeleton_integer_precisionAndRounding() throws {
91107
let style: IntegerFormatStyle<Int> = .init(locale: Locale(identifier: "en_US"))
92108
#expect(style.precision(.fractionLength(3...3)).rounded(increment: 5).collection.skeleton == "precision-increment/5.000 rounding-mode-half-even")

Tests/FoundationInternationalizationTests/Formatting/NumberFormatStyleTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ private struct NumberFormatStyleTests {
337337

338338
}
339339

340+
@Test func decimalFormatStyle_Currency_groupingWithRounding() throws {
341+
let style = Decimal.FormatStyle.Currency(code: "USD", locale: enUSLocale)
342+
343+
_testNegativePositiveDecimal(style.grouping(.never), [ "$87650.00", "$8765.00", "$876.50", "$87.65", "$8.76", "$0.88", "$0.09", "$0.01", "$0.00", "-$0.01", "-$876.50", "-$87650.00" ], "currency grouping(.never)")
344+
_testNegativePositiveDecimal(style.rounded(rule: .toNearestOrEven), [ "$87,650.00", "$8,765.00", "$876.50", "$87.65", "$8.76", "$0.88", "$0.09", "$0.01", "$0.00", "-$0.01", "-$876.50", "-$87,650.00" ], "currency rounded")
345+
346+
_testNegativePositiveDecimal(style.rounded(rule: .toNearestOrEven).grouping(.never), [ "$87650.00", "$8765.00", "$876.50", "$87.65", "$8.76", "$0.88", "$0.09", "$0.01", "$0.00", "-$0.01", "-$876.50", "-$87650.00" ], "currency rounded + grouping(.never)")
347+
_testNegativePositiveDecimal(style.grouping(.never).rounded(rule: .toNearestOrEven), [ "$87650.00", "$8765.00", "$876.50", "$87.65", "$8.76", "$0.88", "$0.09", "$0.01", "$0.00", "-$0.01", "-$876.50", "-$87650.00" ], "currency grouping(.never) + rounded")
348+
}
349+
340350
@Test func decimal_withCustomShorthand() async {
341351
await usingCurrentInternationalizationPreferences {
342352
// This test can only be run with the system set to the en_US language

0 commit comments

Comments
 (0)