Skip to content
Open
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
Next Next commit
Added string test to Doubles and code for .double to handle Strings
  • Loading branch information
Causaelity authored and Causaelity committed Nov 10, 2017
commit eeae5ecfc9faad87ab95c0de6b0a575a6d74effe
6 changes: 6 additions & 0 deletions Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,12 @@ extension JSON {
public var number: NSNumber? {
get {
switch self.type {
case .string:
let decimal: NSDecimalNumber? = NSDecimalNumber(string: self.object as? String)
if decimal == NSDecimalNumber.notANumber { // indicates parse error
return nil
}
return decimal
case .number:
return self.rawNumber
case .bool:
Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftyJSONTests/NumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.boolValue, false)
XCTAssertEqual(json.doubleValue, 0.0)
XCTAssertEqual(json.numberValue, 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json = "9876543210.123456789"
XCTAssertEqual(json.double!, 9876543210.123456789)
XCTAssertEqual(json.doubleValue, 9876543210.123456789)
XCTAssertEqual(json.numberValue, 9876543210.123456789)
XCTAssertEqual(json.stringValue, "9876543210.123456789")
}

func testFloat() {
Expand Down