【发布时间】:2021-10-08 09:59:50
【问题描述】:
我想得到四舍五入的结果,请看附图。它有什么问题。
结果应该是:18.59
我使用了 float 而不是 double。
MRE:
float value = 18.585f;
System.out.println(((float)Math.round(value * 100))/100);
观察到的输出:
18.58
【问题讨论】:
-
System.out.println((float)Math.round(18.585*100)/100);为我返回 18.59
-
双倍值 = 18.585; System.out.println(((double)Math.round(value * 100))/100);打印 18,59........ 浮点值 = 18.585f; System.out.println(((double)Math.round(value * 100))/100);打印 18,58
-
@Petr F. 我使用了 float 而不是 double。
-
使用 float 我得到相同的结果:double value = 18.585; System.out.println(((float)Math.round(value * 100))/100);打印 18,59........ 浮点值 = 18.585f; System.out.println(((float)Math.round(value * 100))/100);打印 18,58
-
我投了反对票,因为您没有将代码粘贴为文本,也因为您没有给我们minimal reproducible example。很高兴你给了我们预期的结果。我们还需要观察到的结果。
标签: java android android-studio java-11