货币计算为什么使用BigDecimal


double d = 29.0 * 0.01;
System.out.println(d);
System.out.println(d * 100);
System.out.println((int) (d * 100));

输出:

0.29
28.999999999999996
28



float f = (float) (29.45*0.01);
System.out.println(f);
System.out.println(f * 100);
System.out.println((int) (f * 100));

输出:

0.2945
29.449999
29



//需要使用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);


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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值