如何在json swift中解析时保持原始double值?

Shi*_*wal 5 double json swift swifty-json

我从服务器收到以下响应

"transactionInfo": {
      "currencyCode": "MYR",
      "total": 472.23,
      "roomCurrencyCode": "MYR",
      "roomTotal": 472.23
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用SwiftyJson进行转换并使用以下代码进行映射时

struct TransactionInfoModel: Codable {
   var currencyCode: String
   var total: Double
   var roomCurrencyCode: String
   var roomTotal: Double
}
Run Code Online (Sandbox Code Playgroud)

然后我得到如下值:

"currencyCode": "MYR",
"total": 472.23000000000002,
"roomCurrencyCode": "MYR",
"roomTotal": 472.23000000000002
Run Code Online (Sandbox Code Playgroud)

请提出如何解决此问题的建议。提前致谢。

小智 -1

Swift 使用 IEEE 754 标准来表示单精度和双精度。

swift:将字符串转换为双 Martining 的问题已经清楚地回答了您的问题。