Skip to content

Commit 6a0c9af

Browse files
authored
Merge pull request #1896 from spevans/pr_nsdecimalnumber_init_42
[4.2] NSDecimalNumber: Add missing init(value:) initialisers.
2 parents a5660a5 + 86c4684 commit 6a0c9af

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Foundation/NSDecimalNumber.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,21 @@ open class NSDecimalNumber : NSNumber {
7979

8080
fileprivate let decimal: Decimal
8181
public convenience init(mantissa: UInt64, exponent: Int16, isNegative: Bool) {
82-
var d = Decimal()
83-
d._exponent = Int32(exponent)
82+
var d = Decimal(mantissa)
83+
d._exponent += Int32(exponent)
8484
d._isNegative = isNegative ? 1 : 0
85-
var man = mantissa
86-
d._mantissa.0 = UInt16(man & 0xffff)
87-
man >>= 4
88-
d._mantissa.1 = UInt16(man & 0xffff)
89-
man >>= 4
90-
d._mantissa.2 = UInt16(man & 0xffff)
91-
man >>= 4
92-
d._mantissa.3 = UInt16(man & 0xffff)
93-
d._length = 4
94-
d.trimTrailingZeros()
95-
// TODO more parts of the mantissa...
9685
self.init(decimal: d)
9786
}
87+
9888
public init(decimal dcm: Decimal) {
9989
self.decimal = dcm
10090
super.init()
10191
}
92+
10293
public convenience init(string numberValue: String?) {
10394
self.init(decimal: Decimal(string: numberValue ?? "") ?? Decimal.nan)
10495
}
96+
10597
public convenience init(string numberValue: String?, locale: Any?) {
10698
self.init(decimal: Decimal(string: numberValue ?? "", locale: locale as? Locale) ?? Decimal.nan)
10799
}

TestFoundation/TestDecimal.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class TestDecimal: XCTestCase {
1111

1212
static var allTests : [(String, (TestDecimal) -> () throws -> Void)] {
1313
return [
14+
("test_NSDecimalNumberInit", test_NSDecimalNumberInit),
1415
("test_AdditionWithNormalization", test_AdditionWithNormalization),
1516
("test_BasicConstruction", test_BasicConstruction),
1617
("test_Constants", test_Constants),
@@ -33,6 +34,18 @@ class TestDecimal: XCTestCase {
3334
]
3435
}
3536

37+
func test_NSDecimalNumberInit() {
38+
XCTAssertEqual(NSDecimalNumber(mantissa: 123456789000, exponent: -2, isNegative: true), -1234567890)
39+
XCTAssertEqual(NSDecimalNumber(decimal: Decimal()).decimalValue, Decimal(0))
40+
XCTAssertEqual(NSDecimalNumber(decimal: Decimal(1)).intValue, 1)
41+
XCTAssertEqual(NSDecimalNumber(string: "1.234").floatValue, 1.234)
42+
XCTAssertTrue(NSDecimalNumber(string: "invalid").decimalValue.isNaN)
43+
XCTAssertEqual(NSDecimalNumber(integerLiteral: 0).intValue, 0)
44+
XCTAssertEqual(NSDecimalNumber(floatLiteral: Double.pi).doubleValue, Double.pi)
45+
XCTAssertEqual(NSDecimalNumber(booleanLiteral: true).boolValue, true)
46+
XCTAssertEqual(NSDecimalNumber(booleanLiteral: false).boolValue, false)
47+
}
48+
3649
func test_AdditionWithNormalization() {
3750

3851
let biggie = Decimal(65536)

0 commit comments

Comments
 (0)