货币计算为什么使用BigDecimal

来源:http://lysunki.iteye.com/blog/1446610


Java代码   收藏代码
  1. double d = 29.0 * 0.01;  
  2. System.out.println(d);  
  3. System.out.println(d * 100);  
  4. System.out.println((int) (d * 100));  

  输出: 
Java代码   收藏代码
  1. 0.29  
  2. 28.999999999999996  
  3. 28  


Java代码   收藏代码
  1. float f = (float) (29.45*0.01);  
  2. System.out.println(f);  
  3. System.out.println(f * 100);  
  4. System.out.println((int) (f * 100));  

  输出: 
Java代码   收藏代码
  1. 0.2945  
  2. 29.449999  
  3. 29  


Java代码   收藏代码
  1. //需要使用String初始化对象  
  2. BigDecimal x = new BigDecimal("500000000.00");  
  3. BigDecimal y = new BigDecimal("0.6");  
  4. BigDecimal z = x.multiply(y);  
  5. System.out.println(">>>>>>>>>> "+z);  
  6. >>>>>>>>>> 300000000.000  
  7.   
  8. BigDecimal x2 = new BigDecimal(500000001);  
  9. BigDecimal y2 = new BigDecimal(0.6);  
  10. BigDecimal z2 = x2.multiply(y2);  
  11. >>>>>>>>>> 300000000.59999998889776973154397410326055251061916351318359375  
  12. System.out.println(">>>>>>>>>> "+z2);  


Java代码   收藏代码
  1. BigDecimal equals   
  2.      * this method considers two BigDecimal objects   
  3.      * equal only if they are equal in value and scale  
  4.      * thus 2.0 is not equal to 2.00 when compared by this method.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值