1.BigInteger类概述:
java.math.BigInteger类 表示一个超大的整数
2.构造方法:
public BigInteger(String val) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger
3.成员方法:
3.1、BigInteger add(BigInteger val) 返回其值为 (this + val) 的 BigInteger。
// 创建一个BigInteger对象
BigInteger b1 = new BigInteger("58723458753458347585823963475267357368578345723578324757329853945923895");
BigInteger b2 = new BigInteger("82372345875345834758582396347526735736857834572357832475732985394523324");
// b1+b2
BigInteger result1 = b1.add(b2);
System.out.println(result1);
3.2、BigInteger subtract(BigInteger val) 返回其值为 (this - val) 的 BigInteger。
// 创建一个BigInteger对象
BigInteger b1 = new BigInteger("58723458753458347585823963475267357368578345723578324757329853945923895");
BigInteger b2 = new BigInteger("82372345875345834758582396347526735736857834572357832475732985394523324");
// b1-b2
BigInteger result2 = b1.subtract(b2);
System.out.println(result2);
3.3、BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。
// 创建一个BigInteger对象
BigInteger b1 = new BigInteger("58723458753458347585823963475267357368578345723578324757329853945923895");
BigInteger b2 = new BigInteger("82372345875345834758582396347526735736857834572357832475732985394523324");
// b1*b2
BigInteger result3 = b1.multiply(b2);
System.out.println(result3);
3.4、BigInteger divide(BigInteger val) 返回其值为 (this / val) 的 BigInteger。除不尽的取整数
// 创建一个BigInteger对象
BigInteger b1 = new BigInteger("58723458753458347585823963475267357368578345723578324757329853945923895");
BigInteger b2 = new BigInteger("82372345875345834758582396347526735736857834572357832475732985394523324");
// b1/b2
BigInteger result4 = b1.divide(b2);
System.out.println(result4);
3.5、long的最高值
// long 最高值
long a = 9223372036854775807L;
System.out.println(a);
4.BigDecimal类概述:
java.math.BigDecimal 表示超大的小数,并且可以解决小数运算的精度问题
5、构造方法:
BigDecimal(double val) 将 double 转换为 BigDecimal,后者是 double 的二进制浮点值准确的十进制表示形式。
BigDecimal(String val) 将 BigDecimal 的字符串表示形式转换为 BigDecimal。
6、成员方法:
BigDecimal add(BigDecimal augend) 返回一个 BigDecimal,其值为 (this + augend)
BigDecimal subtract(BigDecimal subtrahend) 返回一个 BigDecimal,其值为 (this - subtrahend)
BigDecimal multiply(BigDecimal multiplicand) 返回一个 BigDecimal,其值为 (this × multiplicand)
// 小数加法运算
BigDecimal b1 = new BigDecimal("0.09");
BigDecimal b2 = new BigDecimal("0.01");
BigDecimal r1 = b1.add(b2);
System.out.println(r1);
// 小数减法运算
BigDecimal b3 = new BigDecimal("1.0");
BigDecimal b4 = new BigDecimal("0.32");
BigDecimal r2 = b1.subtract(b4);
System.out.println(r2);
// 小数乘法运算
BigDecimal b5 = new BigDecimal("0.19");
BigDecimal b6 = new BigDecimal("0.31");
BigDecimal r3 = b5.multiply(b6);
System.out.println(r3);
// 小数除法运算
BigDecimal b7 = new BigDecimal("1.301");
BigDecimal b8 = new BigDecimal("100");
BigDecimal r4 = b7.divide(b8);
System.out.println(r4);
BigDecimal divide(BigDecimal divisor) 返回一个 BigDecimal,其值为 (this / divisor)
注意:divide方法除不尽
会报错,java.lang.ArithmeticException异常
调用另一个方法(重载)
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
返回一个 BigDecimal,其值为 (this / divisor)
参数1 divisor:除数对应的BigDecimal对象
参数2 scale:精确到位数
参数3 roundingMode:RoundingMode枚举:RoundingMode.HALF_UP 四舍五入
// 有问题的除法运算(除不尽的情况)
BigDecimal b9 = new BigDecimal("10");
BigDecimal b10 = new BigDecimal("3");
BigDecimal r5= b9.divide(b10,5, RoundingMode.HALF_UP);