BigDecimal:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。适用于为了能精确的表示、计算浮点数。
常用方法:- public BigDecimal add(BigDecimal augend) 加
- public BigDecimal subtract(BigDecimal subtrahend) 减
- public BigDecimal multiply(BigDecimal multiplicand)乘
- public BigDecimal divide(BigDecimal divisor)除
- public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,保留scale位小数,舍取规则
BigInteger:大整数,可以运算不在Integer范围内的数据
举例:System.out.println(Integer.MAX_VALUE); BigInteger a = new BigInteger("4147483648"); System.out.println("a:" + a); 输出:2147483647a:4147483648
常用方法:
- public BigInteger add(BigInteger val):加
- public BigInteger subtract(BigInteger val):减
- public BigInteger multiply(BigInteger val):乘
- public BigInteger divide(BigInteger val):除
- public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
BigInteger b1 = new BigInteger("12"); BigInteger b2 = new BigInteger("5"); // public BigInteger add(BigInteger val):加 System.out.println("add:" + b1.add(b2)); // public BigInteger subtract(BigInteger val):减 System.out.println("subtract:" + b1.subtract(b2)); // public BigInteger multiply(BigInteger val):乘 System.out.println("multiply:" + b1.multiply(b2)); // public BigInteger divide(BigInteger val):除,返回商 System.out.println("divide:" + b1.divide(b2)); // public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组 BigInteger[] b = b1.divideAndRemainder(b2); System.out.println("商:" + b[0]); System.out.println("余数:" + b[1]); 输出 add:17 subtract:7 multiply:60 divide:2 商:2 余数:2