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
Comments
@swift-ci create |
Comment by Kyle Macomber (JIRA) I can't reproduce on my Mac:
Perhaps this is specific to corelibs-foundation? |
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:- |
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. |
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). |
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 |
Comment by Kyle Macomber (JIRA) I see, that makes sense. Re-opening. |
Comment by Adriano Gonçalves (JIRA) Isn't this issue a duplicate of rdar://problem/33491336 ? |
Additional Detail from JIRA
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.
The text was updated successfully, but these errors were encountered: