Skip to content

SR-7481: NumberFormatter inconsistency on Linux #1530

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 2 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions Foundation/NumberFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ open class NumberFormatter : Formatter {
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterPlusSign, value: _plusSign?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: _currencySymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterExponentSymbol, value: _exponentSymbol?._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinIntegerDigits, value: _minimumIntegerDigits._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinIntegerDigits, value: minimumIntegerDigits._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMaxIntegerDigits, value: _maximumIntegerDigits._bridgeToObjectiveC()._cfObject)
_setFormatterAttribute(formatter, attributeName: kCFNumberFormatterMinFractionDigits, value: _minimumFractionDigits._bridgeToObjectiveC()._cfObject)
if _minimumFractionDigits <= 0 {
Expand Down Expand Up @@ -190,12 +190,15 @@ open class NumberFormatter : Formatter {
case .currency, .currencyPlural, .currencyISOCode, .currencyAccounting:
_usesSignificantDigits = false
_usesGroupingSeparator = true
if _minimumIntegerDigits == nil {
_minimumIntegerDigits = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I believe this value is 0 for .currencyPlural on Darwin. Likewise, I see the value being 1 for .percent, which I believe is falling into the default case (along with .scientific, which is correctly 0).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add some more test cases to compare against native Foundation and fix up these others.

}
_minimumFractionDigits = 2

case .decimal:
_usesGroupingSeparator = true
_maximumFractionDigits = 3
if _minimumIntegerDigits == 0 {
if _minimumIntegerDigits == nil {
_minimumIntegerDigits = 1
}
if _groupingSize == 0 {
Expand Down Expand Up @@ -680,11 +683,14 @@ open class NumberFormatter : Formatter {
_roundingIncrement = newValue
}
}

internal var _minimumIntegerDigits: Int = 0

// Use an optional for _minimumIntegerDigits to track if the value is
// set BEFORE the .numberStyle is changed. This allows preserving a setting
// of 0.
internal var _minimumIntegerDigits: Int?
open var minimumIntegerDigits: Int {
get {
return _minimumIntegerDigits
return _minimumIntegerDigits ?? 0
}
set {
_reset()
Expand Down
22 changes: 22 additions & 0 deletions TestFoundation/TestNumberFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ class TestNumberFormatter: XCTestCase {
XCTAssertEqual(numberFormatter.minimumIntegerDigits, 3)
formattedString = numberFormatter.string(from: 0.1)
XCTAssertEqual(formattedString, "000.1")

numberFormatter.numberStyle = .currency
XCTAssertEqual(numberFormatter.minimumIntegerDigits, 3)

// If .minimumIntegerDigits is set to 0 before .numberStyle change, preserve the value
let currencyFormatter = NumberFormatter()
XCTAssertEqual(currencyFormatter.minimumIntegerDigits, 0)
currencyFormatter.minimumIntegerDigits = 0
currencyFormatter.numberStyle = .currency
XCTAssertEqual(currencyFormatter.minimumIntegerDigits, 0)
currencyFormatter.locale = Locale(identifier: "en_US")
formattedString = currencyFormatter.string(from: NSNumber(value: 0))
XCTAssertEqual(formattedString, "$.00")

// If .minimumIntegerDigits is not set before .numberStyle change, update the value
let currencyFormatter2 = NumberFormatter()
XCTAssertEqual(currencyFormatter2.minimumIntegerDigits, 0)
currencyFormatter2.numberStyle = .currency
XCTAssertEqual(currencyFormatter2.minimumIntegerDigits, 1)
currencyFormatter2.locale = Locale(identifier: "en_US")
formattedString = currencyFormatter2.string(from: NSNumber(value: 0))
XCTAssertEqual(formattedString, "$0.00")
}

func test_maximumIntegerDigits() {
Expand Down