Java代码 收藏代码
double d = 29.0 * 0.01;
System.out.println(d);
System.out.println(d * 100);
System.out.println((int) (d * 100));
输出:
Java代码 收藏代码
0.29
28.999999999999996
28
Java代码 收藏代码
float f = (float) (29.45*0.01);
System.out.println(f);
System.out.println(f * 100);
System.out.println((int) (f * 100));
输出:
Java代码 收藏代码
0.2945
29.449999
29
Java代码 收藏代码
//需要使用String初始化对象
BigDecimal x = new BigDecimal("500000000.00");
BigDecimal y = new BigDecimal("0.6");
BigDecimal z = x.multiply(y);
System.out.println(">>>>>>>>>> "+z);
>>>>>>>>>> 300000000.000
BigDecimal x2 = new BigDecimal(500000001);
BigDecimal y2 = new BigDecimal(0.6);
BigDecimal z2 = x2.multiply(y2);
>>>>>>>>>> 300000000.59999998889776973154397410326055251061916351318359375
System.out.println(">>>>>>>>>> "+z2);
Java代码 收藏代码
BigDecimal equals
* this method considers two BigDecimal objects
* equal only if they are equal in value and scale
* thus 2.0 is not equal to 2.00 when compared by this method.