可能重复:
Floating point arithmetic not producing exact results in Java
import java.io.*;
class Test
{
void calc()
{
double d = 0.1001;
for(int i = 0; i <= 10;i++)
{
System.out.println(d*10);
d = d*10;
}
}
}
当我运行此文件时,我得到以下输出:
1.001
10.009999999999998
100.09999999999998
1000.9999999999998
10009.999999999998
100099.99999999999
1000999.9999999999
1.0009999999999998E7
1.0009999999999999E8
1.0009999999999999E9
1.0009999999999998E10
而不是我想要的输出:
1.001
10.01
100.1
等...
我需要知道为什么会发生这种情况?