Skip to content

Commit 031e824

Browse files
David DunnDavid Dunn
authored andcommitted
Updated NumberFormatter to match expected behavior
1 parent e3f514d commit 031e824

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

Foundation/NSNumberFormatter.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ open class NumberFormatter : Formatter {
193193
_minimumFractionDigits = 2
194194

195195
case .decimal:
196-
_usesSignificantDigits = false
197196
_usesGroupingSeparator = true
198197
_maximumFractionDigits = 3
199198
_minimumIntegerDigits = 1
@@ -783,6 +782,7 @@ open class NumberFormatter : Formatter {
783782
}
784783
set {
785784
_reset()
785+
_usesSignificantDigits = true
786786
_minimumSignificantDigits = newValue
787787
}
788788
}
@@ -794,6 +794,7 @@ open class NumberFormatter : Formatter {
794794
}
795795
set {
796796
_reset()
797+
_usesSignificantDigits = true
797798
_maximumSignificantDigits = newValue
798799
}
799800
}

TestFoundation/TestNSNumberFormatter.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,22 +369,22 @@ class TestNSNumberFormatter: XCTestCase {
369369

370370
func test_minimumSignificantDigits() {
371371
let numberFormatter = NumberFormatter()
372-
numberFormatter.usesSignificantDigits = true
372+
numberFormatter.numberStyle = .decimal
373373
numberFormatter.minimumSignificantDigits = 3
374374
let formattedString = numberFormatter.string(from: 42)
375375
XCTAssertEqual(formattedString, "42.0")
376376
}
377377

378378
func test_maximumSignificantDigits() {
379379
let numberFormatter = NumberFormatter()
380+
numberFormatter.numberStyle = .decimal
381+
var formattedString = numberFormatter.string(from: 987654321)
382+
XCTAssertEqual(formattedString, "987,654,321")
383+
380384
numberFormatter.usesSignificantDigits = true
381385
numberFormatter.maximumSignificantDigits = 3
382-
var formattedString = numberFormatter.string(from: 42.42424242)
386+
formattedString = numberFormatter.string(from: 42.42424242)
383387
XCTAssertEqual(formattedString, "42.4")
384-
385-
numberFormatter.numberStyle = .decimal
386-
formattedString = numberFormatter.string(from: 987654321)
387-
XCTAssertEqual(formattedString, "987,654,321")
388388
}
389389

390390
func test_stringFor() {
@@ -404,6 +404,9 @@ class TestNSNumberFormatter: XCTestCase {
404404
XCTAssertEqual(numberFormatter.string(for: 3), "three")
405405
XCTAssertEqual(numberFormatter.string(for: 0.3), "zero point three")
406406

407+
numberFormatter.numberStyle = .decimal
408+
XCTAssertEqual(numberFormatter.string(for: 234.5678), "234.568")
409+
407410
numberFormatter.locale = Locale(identifier: "zh_CN")
408411
numberFormatter.numberStyle = .spellOut
409412
XCTAssertEqual(numberFormatter.string(from: 11.4), "十一点四")

0 commit comments

Comments
 (0)