Skip to content

Commit 70f8af9

Browse files
authored
Merge pull request #2628 from spevans/pr_sr_12036_51
[5.1] SR-12036: Incorrect and Inconsistent NumberFormatter currency behavior on Linux
2 parents 0af915c + 255fbc8 commit 70f8af9

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Foundation/NumberFormatter.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,14 @@ open class NumberFormatter : Formatter {
122122

123123
private func _setFormatterAttributes(_ formatter: CFNumberFormatter) {
124124
if numberStyle == .currency {
125-
let symbol = _currencySymbol ?? _currencyCode ?? locale.currencySymbol ?? locale.currencyCode
126-
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol?._cfObject)
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 {
129+
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code._cfObject)
130+
} else {
131+
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: locale.currencySymbol?._cfObject)
132+
}
127133
}
128134
if numberStyle == .currencyISOCode {
129135
let code = _currencyCode ?? _currencySymbol ?? locale.currencyCode ?? locale.currencySymbol

TestFoundation/TestNumberFormatter.swift

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

269269
let formattedString = numberFormatter.string(from: 42)
270270
XCTAssertEqual(formattedString, "T42_00")
271+
272+
// Check that the currencyCode is preferred over the locale when no currencySymbol is set
273+
let codeFormatter = NumberFormatter()
274+
codeFormatter.numberStyle = .currency
275+
codeFormatter.locale = Locale(identifier: "en_US")
276+
codeFormatter.currencyCode = "GBP"
277+
XCTAssertEqual(codeFormatter.string(from: 3.02), "£3.02")
271278
}
272279

273280
func test_decimalSeparator() {

0 commit comments

Comments
 (0)