首页 > 解决方案 > 我在 Java 中看到双重算术的意外和不受欢迎的行为

问题描述

好的,JavaSE 1.8 在 Eclipse 版本中运行应用程序:Oxygen.3a Release (4.7.3a) Build id:20180405-1200。简单的算术:

Double dblBalance; ( showing as 2072.41 in debug, but also running the app in the IDE)

Double dblAmountofAllocation; ( showing as 1900.0 at runtime )

dblBalance = dblBalance - dblAmountofAllocation; ( dblBalance shows 172.40999995 at runtime)
( my workaround - shouldn't be necessary)
dblBalance = Math.round(dblBalance * 100.0) / 100.0;

我还在从命令行编译和运行的测试用例中对此进行了测试。所以它是可重现的。它是 Java 运行时引擎吗?

非常困惑 - 我想知道这是否可以被其他人复制。大多数 Double 值的组合都没有这个问题 - 我需要坚持使用本机数据类型吗?

标签: javadoublearithmetic-expressions

解决方案


推荐阅读