Skip to content

Commit 415d1a6

Browse files
authored
Merge pull request #2622 from spevans/pr_sr_12036_52
[5.2] SR-12036: Incorrect and Inconsistent NumberFormatter currency behavior on Linux
2 parents 53303da + b9a6b7c commit 415d1a6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Foundation/NumberFormatter.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ open class NumberFormatter : Formatter {
122122

123123
private func _setFormatterAttributes(_ formatter: CFNumberFormatter) {
124124
if numberStyle == .currency {
125-
let symbol = _currencySymbol ?? locale.currencySymbol
126-
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol?._cfObject)
127-
128-
if let code = _currencyCode, code.count == 3 {
125+
// Prefer currencySymbol, then currencyCode then locale.currencySymbol
126+
if let symbol = _currencySymbol {
127+
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol._cfObject)
128+
} else if let code = _currencyCode, code.count == 3 {
129129
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code._cfObject)
130+
} else {
131+
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: locale.currencySymbol?._cfObject)
130132
}
131133
}
132134
if numberStyle == .currencyISOCode {

TestFoundation/TestNumberFormatter.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ class TestNumberFormatter: XCTestCase {
273273

274274
let formattedString = numberFormatter.string(from: 42)
275275
XCTAssertEqual(formattedString, "£42_00")
276+
277+
// Check that the currencyCode is preferred over the locale when no currencySymbol is set
278+
let codeFormatter = NumberFormatter()
279+
codeFormatter.numberStyle = .currency
280+
codeFormatter.locale = Locale(identifier: "en_US")
281+
codeFormatter.currencyCode = "GBP"
282+
XCTAssertEqual(codeFormatter.string(from: 3.02), "£3.02")
276283
}
277284

278285
func test_decimalSeparator() {

0 commit comments

Comments
 (0)