How to keep original double value while parsing in json swift?
Asked Answered
C

1

8

I received the following response from server

"transactionInfo": {
      "currencyCode": "MYR",
      "total": 472.23,
      "roomCurrencyCode": "MYR",
      "roomTotal": 472.23
}

but when I am trying to convert using SwiftyJson and map using below code

struct TransactionInfoModel: Codable {
   var currencyCode: String
   var total: Double
   var roomCurrencyCode: String
   var roomTotal: Double
}

then I get values like below:

"currencyCode": "MYR",
"total": 472.23000000000002,
"roomCurrencyCode": "MYR",
"roomTotal": 472.23000000000002

Please suggest how to resolve this issue. Thanks in advance.

Callen answered 5/2, 2018 at 2:30 Comment(4)
Double can’t represent certain values exactly. Use Decimal, slower but safer for financial dataEscarpment
@CodeDifferent Thanx but I changed Double to Decimal but still facing the same issue.Callen
I have debugged the code and find out that double value gets change while converting to JSON using SwiftyJson.Callen
github.com/SwiftyJSON/SwiftyJSON/issues/930Seleta
I
-1

Swift uses the IEEE 754 standard for representing single- and double-precision.

swift: issue in converting string to double Marting has answered clearly your question.

Irfan answered 20/5, 2020 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.