double 转 NSString 出现精度异常 #4
onnttf
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
在日常开发中,后台再给我们返回数字类型的数据时,很有可能是带小数点的。我们只能用
float
或者double
去接,我们如果需要显示给用户时,就会造成精度异常。错误事例
上面是服务端给我们返回的
response
,我们将它转为字典。到这,也许有人会说:在转字符串时,加上保留的位数就可以了。但其实这样做并不是太合理。因为我们根本不服务端后台会给我们返回什么数据,所以我们不知道该保留几位小数。同理,我们也不能采取切割字符串的方式去保留小数位数。
解决办法
采用苹果提供的
NSDecimalNumber
解决此问题。本文的所有代码均以上传至
GitHub
,如需自取~Beta Was this translation helpful? Give feedback.
All reactions