27.1 BigInteger 和 BigDecima 介绍
- BigInteger 适合保存比较大的整型
- BigDecimal 适合保存精度更高的浮点型(小数)
27.2 BigInteger 应用
BigInteger bigInteger1 = new BigInteger("2315456456456561515121212121115");
BigInteger bigInteger2 = new BigInteger("1524154515415641564564564161515615615151");
- 加
BigInteger add = bigInteger1.add(bigInteger2);
- 减
BigInteger subtract = bigInteger1.subtract(bigInteger2);
- 乘
BigInteger multiply = bigInteger1.multiply(bigInteger2);
- 除
BigInteger divide = bigInteger2.divide(bigInteger1);
除完是保留整型,没有四舍五入
27.3 BigDecimal 应用
BigDecimal bigDecimal1 = new BigDecimal("23.154515415456456158884415154845156464845");
BigDecimal bigDecimal2 = new BigDecimal("12.4545254");
- 加
BigDecimal add = bigDecimal1.add(bigDecimal2);
- 减
BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
- 乘
BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);
- 除
BigDecimal divide = bigDecimal1.divide(bigDecimal2, MathContext.DECIMAL32);
可能会抛出异常-ArithmeticException
所以在调用该方法时,指定精度即可
最后
以上就是活力奇迹最近收集整理的关于27.Java之BigInteger和BigDecimal(加、减、乘、除运算)的全部内容,更多相关27内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复