为什么double类型会这样?

时间:2015-01-28 14:00:50

标签: java double precision

查看以下代码。 为什么0.01D常量会使输出变得如此不准确?

double x = 0D;
while (x < 2D) {
    System.out.println(x);
    x = x + 0.01D;
}
  

0.0
  0.01
  0.02
  0.03
  0.04
  0.05
  0.060000000000000005
  0.07
  0.08
  0.09
  0.09999999999999999
  0.10999999999999999
  0.11999999999999998
  0.12999999999999998
  0.13999999999999999
  0.15

0 个答案:

没有答案