日期:2014-05-20  浏览次数:20532 次

为何2.00-1.10=0.89...9
public class Class1 {
public static void main(String []s){
System.out.println(2.00-1.10);

System.out.println(1.10+0.899);

double ss = 2.00;
double n = 1.10;
System.out.println(ss - n); // 为何double还是0.899999999999,float就是0.9;


float s2 = (float) 2.00;
float nn = (float) 1.10;
System.out.print(s2 - nn);

}

}



------解决方案--------------------
精度问题,float类型的变量只有7位的精度,而double类型的变量有15位的精度。所以用float时超出了精度范围,自动舍入处理了!但是用double时没有超出范围!
------解决方案--------------------
0.1,无法用二进制精确表示
如果N是负数,10的N次幂无法用二进制精确表示。
既然不精确,差一点点,可以理解。
------解决方案--------------------
原因可以参考下大海哥的。
在JAVA中要取得精确的带小数的值,用BigDecimal吧