android - 为什么不能使用 Math.round 正确舍入?
问题描述
为什么我不能正确四舍五入:
val solution = Math. round(iDouble * 10.00) / 10.00
调试器说:
iDouble = 118.64300000000001
solution = 118.6
解决方案
我看不出有什么问题。这是解释。
118.64300000000001d * 10L = 1186.43d
Math.round 将舍入为整数部分的数字作为长整数返回。
数学.round(1186.43d) = 1186L。
1186L / 10.00d = 118.6d
所以我觉得很好。请记住,除以双精度/浮点数将导致双精度/浮点数。
推荐阅读
- vba - VBA中的Writeline因文本太长而中断
- ios - Swift 视图未显示
- sql - 获取列中值的百分比
- java - 什么时候调用 Thread.currentThread().interrupt() 什么时候不调用?
- node.js - 如何获取环境变量并保存到参数存储?
- akka-http - 将 Spray Deserializer 转换为 Akka-Http Unarshaller
- java - 尝试使用 Intent 获取字符串时未设置 .child()
- javascript - 如何向本地服务器发出 axios 请求
- facebook-graph-api - 如何获取 Facebook 帖子评论?
- javascript - 如何使用 Javascript 总共添加 10% 的折扣