Skip to content

Commit d5b67e5

Browse files
authored
Merge pull request #2614 from spevans/pr_sr_12036
SR-12036: Incorrect and Inconsistent NumberFormatter currency behavior on Linux
2 parents 8d009ae + 187d6b3 commit d5b67e5

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
@@ -113,11 +113,13 @@ open class NumberFormatter : Formatter {
113113

114114
private func _setFormatterAttributes(_ formatter: CFNumberFormatter) {
115115
if numberStyle == .currency {
116-
let symbol = _currencySymbol ?? locale.currencySymbol
117-
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol?._cfObject)
118-
119-
if let code = _currencyCode, code.count == 3 {
116+
// Prefer currencySymbol, then currencyCode then locale.currencySymbol
117+
if let symbol = _currencySymbol {
118+
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol._cfObject)
119+
} else if let code = _currencyCode, code.count == 3 {
120120
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code._cfObject)
121+
} else {
122+
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: locale.currencySymbol?._cfObject)
121123
}
122124
}
123125
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)