I get the following JSON response from an API.
{
"status": "success",
"data": [
{
"actual_price": 30,
"offered_deal_price": 16,
"pending_balance": 12.8
}
]
}
All those values are prices. Which means they can be round values or floating values.
I read that you should use NSDecimalNumber
type for currency values. I'm having trouble converting these JSON values.
Doing this,
json["pending_balance"] as! NSDecimalNumber
failed with the following error.
Could not cast value of type '__NSCFNumber' (0x10423ccf0) to 'NSDecimalNumber' (0x1046cf1f0)
Trying to cast it to NSDecimal
resulted in this
Could not cast value of type '__NSCFNumber' (0x7f9102f79f68) to 'C.NSDecimal' (0x10f23d6e0).
I can however, cast it to Swift types like Float
or Double
without an issue.
Anyone got an idea what's the problem with NSDecimalNumber
? Or is it safe to go ahead with Float
or Double
? If so which one is the best for currency values?
Thank you.