我是靠谱客的博主 忧伤凉面,这篇文章主要介绍货币计算为什么使用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.

最后

以上就是忧伤凉面最近收集整理的关于货币计算为什么使用BigDecimal的全部内容,更多相关货币计算为什么使用BigDecimal内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(116)

评论列表共有 0 条评论

立即
投稿
返回
顶部