gpt4 book ai didi

java - Math.round(double * Math.pow(10, int)) 的两个不同输出

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:36 25 4
gpt4 key购买 nike

我遇到了一个非常奇怪的问题。我从同一个操作中得到两个不同的输出。此处描述了问题。

double num = 2.0170818E7; 
int dp = 4;

String s = (Math.round(num * Math.pow(10, dp)) / (int) Math.pow(10, dp)) + "";

当我在 Android 5.0+ 中运行上述代码集时,我得到的输出为:

正确的输出:

20170818

当我在 Android 5.0(我尝试使用 4.4.2 版)下运行上述代码集时,我得到的输出为:

不正确的输出:

20172835

正确的输出是 20170818 或预期的输出。为什么我会遇到这个奇怪的问题?

最佳答案

原来Android开发工具只支持jdk 6的特性。但情况发生了变化,首先是 Android Studio 0.3.2 于 2013 年 10 月发布。如果您的目标是 Android 5.0 及更高版本,则需要 Java 7 或更高版本。现在可以使用 java 8 的一些特性。

JDK 8 在问题 JDK-7131459: DecimalFormat produces wrong format() results when close to a tie 中对 DecimalFormat 舍入进行了有意更改.

但是,这些更改引入了一个真正的错误,记录为 JDK-8039915: Wrong NumberFormat.format() HALF_UP rounding when last digit exactly at rounding position greater than 5 .例如:

99.9989 -> 100.00
99.9990 -> 99.99

Oracle 在 Java 8 更新 40 中修复了这个错误

  • Bug 8039915跟踪 OpenJDK [Java 9 修复][JDK9 补丁]

关于java - Math.round(double * Math.pow(10, int)) 的两个不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com