I have this Info.
let params2: [String: AnyObject] = [
"app_token": myapptoken,
"member_access_token": accessToken!,
"pay_process": 0,
"payamount_credit": 9.87 //hardcode
]
When print params2
The result is
["app_token": myapptoken, "member_access_token": accessToken, "payamount_credit": 9.869999999999999, "pay_process": 0]
The "payamount_credit": 9.87
now is "payamount_credit": 9.869999999999999
I have tried all the ways that exist round but behaves the same.
NSString(format: "%.\2f", 9.87)
Double(round(1000*9.87)/1000)
The strangest thing of all is that only happens with this specific number (9.87), is something mystical.
Playground Screen.
The problem lies within the numeric precision, simply put, some numbers cannot be defined as a Double
without losing precision.
Instead you should use NSDecimalNumber:
let num = NSDecimalNumber(string: "9.87")