大数处理方案(BigInteger和BigDecimal)

BigInteger适合保存比较大的数据,比如处理很大的数据,long不够用,可以使用BigInteger搞定,在对BigInteger进行操作时,需要使用对应的方法,不能直接进行 + - * /

例如BigInteger bigInteger1 = new BigInteger("278999999999999999999999999999");

BigInteger bigInteger2 = new BigInteger("1");

BigInteger add = bigInteger1.add(bigInteger2); //278000000000000000000000000000

BigInteger subtract = bigInteger1.subtract(bigInteger2); //278999999999999999999999999998

BigInteger multiply = bigInteger1.multiply(bigInteger2);  //278999999999999999999999999999 * 1

BigInteger divide = bigInteger1.divide(bigInteger2); //278999999999999999999999999999 / 1

divide方法可能会抛出算术异常

----------------------------------

BigDecimal适合保存精度更高的浮点型(小数),比如double不够用的情况下

如果要对BigDecimal进行运算,也需要调用对应的方法

其中调用divide方法可能抛出算术运算异常,可以指定精度即可

bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);

如果有无限循环小数,则保留分子的精度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值