3.9 大数

如果基本的精度不满足你的要求,那么你就要实用BigInteger和BigDecimal类来进行计算数字了,这两个类都是进行精确计算的。
值得注意的是,不能够使用运算符直接进行这两个类的计算操作,得用方法。

BigInteger a = BigInteger.valueOf(5000000000L);
BigInteger b = BigInteger.valueOf(10000000000000L);
BigInteger c = a.add(b);//c = a + b;
BigInteger d = c.multiply(a.add(BigInteger.valueOf(876)));//d = c * (a + 876)
System.out.println(c);//10005000000000
System.out.println(d);//50025008764380000000000

BigInteger类的一些方法

  • BigInteger add(BigInteger other)
  • BigInteger subtract(BigInteger other)
  • BigInteger multiply(BigInteger other)
  • BigInteger divide(BigInteger other)
  • BigInteger mod(BigInteger other)
    和、差、积、商、余
  • int compareTo(BigInteger other)
    0代表相等,负数代表本BigInteger小于other,正数相反
  • static BigInteger valueOf(long x)
    用x初始值产生一个BigInteger对象

BigDecimal类的一些方法

  • BigDecimal add(BigDecimal other)
  • BigDecimal subtract(BigDecimal other)
  • BigDecimal multiply(BigDecimal other)
  • BigDecimal divide(BigDecimal other, RoundingMode mode)
    和、差、积、商,为了计算商,必须选择小数部分的舍入方式
  • int compareTo(BigDecimal other)
    0代表相等,负数代表本BigDecimal小于other,正数相反
  • static BigDecimal valueOf(long x)
    用x初始值产生一个BigDecimal对象
  • static BigDecimal valueOf(long x, int scale)
    用x初始值产生一个BigDecimal对象,x/10E scale
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值