Skip to content

[5.2] SR-12036: Incorrect and Inconsistent NumberFormatter currency behavior on Linux #2622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Foundation/NumberFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ open class NumberFormatter : Formatter {

private func _setFormatterAttributes(_ formatter: CFNumberFormatter) {
if numberStyle == .currency {
let symbol = _currencySymbol ?? locale.currencySymbol
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol?._cfObject)

if let code = _currencyCode, code.count == 3 {
// Prefer currencySymbol, then currencyCode then locale.currencySymbol
if let symbol = _currencySymbol {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol._cfObject)
} else if let code = _currencyCode, code.count == 3 {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code._cfObject)
} else {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: locale.currencySymbol?._cfObject)
}
}
if numberStyle == .currencyISOCode {
Expand Down
7 changes: 7 additions & 0 deletions TestFoundation/TestNumberFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ class TestNumberFormatter: XCTestCase {

let formattedString = numberFormatter.string(from: 42)
XCTAssertEqual(formattedString, "£42_00")

// Check that the currencyCode is preferred over the locale when no currencySymbol is set
let codeFormatter = NumberFormatter()
codeFormatter.numberStyle = .currency
codeFormatter.locale = Locale(identifier: "en_US")
codeFormatter.currencyCode = "GBP"
XCTAssertEqual(codeFormatter.string(from: 3.02), "£3.02")
}

func test_decimalSeparator() {
Expand Down