如果基本的精度不满足你的要求,那么你就要实用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
1567

被折叠的 条评论
为什么被折叠?



