@Test
public void testT() {
double a = 1.8;
double b = 0.1;
double c = 1.9;
Double d = 0.0;
Double e = 0.00;
System.out.println((1.7 + 0) - 1.7);
System.out.println((1.7 + 0.1) - 1.8);
[color=red]System.out.println((a + b) - c);[/color]
System.out.println(NumberUtil.round((a + b) - c));
System.out.println(NumberUtil.round(a + b) - c);
System.out.println(1.9 - 1.9);
System.out.println(d.doubleValue());
System.out.println(e.doubleValue());
}
结果:
0.0
0.0
2.220446049250313E-16
0.0
0.0
0.0
0.0
0.0
两个double 值相加并不一定相等
如:
printf("%.55f", 1.9)
# 1.8999999999999999111821580299874767661094665527343750000
printf("%.55f", 1.8 + 0.1)
# 1.9000000000000001332267629550187848508358001708984375000
得: (1.8 + 0.1) - 1.9 = 2.22044604925031e-16
设计数据库时,尽量使用decimal/numeric, 而避免使用float,double 作为金额等的计算。
public void testT() {
double a = 1.8;
double b = 0.1;
double c = 1.9;
Double d = 0.0;
Double e = 0.00;
System.out.println((1.7 + 0) - 1.7);
System.out.println((1.7 + 0.1) - 1.8);
[color=red]System.out.println((a + b) - c);[/color]
System.out.println(NumberUtil.round((a + b) - c));
System.out.println(NumberUtil.round(a + b) - c);
System.out.println(1.9 - 1.9);
System.out.println(d.doubleValue());
System.out.println(e.doubleValue());
}
结果:
0.0
0.0
2.220446049250313E-16
0.0
0.0
0.0
0.0
0.0
两个double 值相加并不一定相等
如:
printf("%.55f", 1.9)
# 1.8999999999999999111821580299874767661094665527343750000
printf("%.55f", 1.8 + 0.1)
# 1.9000000000000001332267629550187848508358001708984375000
得: (1.8 + 0.1) - 1.9 = 2.22044604925031e-16
设计数据库时,尽量使用decimal/numeric, 而避免使用float,double 作为金额等的计算。