Skip to content
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

[SR-12244] Decimal precision parsing error #4398

Open
swift-ci opened this issue Feb 20, 2020 · 8 comments
Open

[SR-12244] Decimal precision parsing error #4398

swift-ci opened this issue Feb 20, 2020 · 8 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-12244
Radar rdar://problem/64763767
Original Reporter pabitranjan13 (JIRA User)
Type Bug
Status Reopened
Resolution
Additional Detail from JIRA
Votes 4
Component/s Foundation
Labels Bug
Assignee kylemacomber (JIRA)
Priority Medium

md5: ab9f7e987dbfd920de727f454a38f229

Issue Description:

I am trying to find a way to parse a number as Decimal without losing the number's precision.

It seems that the JSON decoder parses it as Double then converts it to Decimal which introduces errors in the parsing. That behavior is in fact incorrect.

ex: - If want to parse the 1.43 from the json response, then it is resulting as 1.4299999999.

But it is suppose to 1.43 always.

@hborla
Copy link
Member

hborla commented Feb 21, 2020

@swift-ci create

@swift-ci
Copy link
Contributor Author

Comment by Kyle Macomber (JIRA)

I can't reproduce on my Mac:

import Foundation

struct Foo: Codable {
 var decimal: Decimal
}

let json = "\{ \"decimal\": 1.43 }".data(using: .utf8)!

let decoder = JSONDecoder()
let foo = try decoder.decode(Foo.self, from: json)

print(foo.decimal) // Prints "1.43" on Darwin

Perhaps this is specific to corelibs-foundation?

@swift-ci
Copy link
Contributor Author

Comment by Pabitra Ranjan Sahu (JIRA)

@kyla Macomber, I can see this is not reproducible but when I am trying to parse JSON from API response, then I can see this.

Please look into this Github reproducible link here:-

SwiftyJSON/SwiftyJSON#930

@swift-ci
Copy link
Contributor Author

Comment by Kyle Macomber (JIRA)

It's my understanding that JSON does not have first class support for decimal numbers and the best practice is to encode decimal numbers as Strings rather than Doubles in JSON.

It appears the issue against SwiftyJSON you linked to was closed for this reason.

@swift-ci
Copy link
Contributor Author

swift-ci commented May 1, 2020

Comment by Mattt (JIRA)

kylemacomber (JIRA User) - My understanding is that JSON specifies only the representation of numbers, not how any particular implementation interprets them. If that's indeed the case, any practice of encoding decimal values as strings is a workaround for mistakes or limitations in those implementations. (Granted, JSON parsing is a minefield, so what passes as "correct behavior" is debatable).

@swift-ci
Copy link
Contributor Author

swift-ci commented May 1, 2020

Comment by Mattt (JIRA)

Quick follow-up: Here's a reproducible example:

import Foundation
struct Foo: Codable { 
  var decimal: Decimal 
}
let json = "{ \"decimal\": 27.31 }".data(using: .utf8)!
let decoder = JSONDecoder() 
let foo = try decoder.decode(Foo.self, from: json)
print(foo.decimal) // Prints "27.30999999999999488" on macOS

@swift-ci
Copy link
Contributor Author

Comment by Kyle Macomber (JIRA)

I see, that makes sense. Re-opening.

@swift-ci
Copy link
Contributor Author

Comment by Adriano Gonçalves (JIRA)

Isn't this issue a duplicate of rdar://problem/33491336 ?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants