目录
为什么需要大数计算
java的int、float、double存放数据的范围是有限的,当存储非常大的数值时会发生溢出。
典型溢出场景:金融、高等数学。
BigInteger类和BigDecimal类,分别用来表示大整数类和大浮点数类,且在理论上只要内存足够大就能够计算、表示无限大的数。
BigDecimal
/*
初始化两个对象
*/
BigDecimal bigDecimal1 = new BigDecimal("4.321");
BigDecimal bigDecimal2 = new BigDecimal("1.234");
加法
/*
加法
输出结果:5.555
*/
System.out.println(bigDecimal1.add(bigDecimal2));
减法
/*
减法
输出结果:3.087
*/
System.out.println(bigDecimal1.subtract(bigDecimal2));
乘法
/*
乘法
输出结果:5.332114
*/
System.out.println(bigDecimal1.multiply(bigDecimal2));
除法
/*
除法
除法存在除不尽的情况,所以需要表明小数点后留几位,不然会报异常。
参数2:小数点后留几位
参数3:
BigDecimal.ROUND_DOWN:直接省略多余的小数,比如1.28如果保留1位小数,得到的就是1.2
BigDecimal.ROUND_UP:直接进位,比如1.21如果保留1位小数,得到的就是1.3
BigDecimal.ROUND_HALF_UP:四舍五入,2.35保留1位,变成2.4
BigDecimal.ROUND_HALF_DOWN:四舍五入,2.35保留1位,变成2.3
后边两种的区别就是如果保留的位数的后一位如果正好是5的时候,一个舍弃掉,一个进位。
输出结果:3.502
*/
System.out.println(bigDecimal1.divide(bigDecimal2, 2, BigDecimal.ROUND_HALF_UP));
绝对值
/*
绝对值
输出结果:4.321
*/
System.out.println(bigDecimal1.abs());
负值
/*
负值 绝对值的对立面
输出结果:-4.321
*/
System.out.println(bigDecimal1.negate());
除法和取模
/*
除和取模 返回一个BigDecimal的数组
输出结果:3,0.619
*/
BigDecimal bigDecimals[] = bigDecimal1.divideAndRemainder(bigDecimal2);
System.out.println(bigDecimals[0] + "," + bigDecimals[1]);
取模
/*
取模
输出结果:0.619
*/
System.out.println(bigDecimal1.remainder(bigDecimal2));
除法取整
/*
除法取整
输出结果:3
*/
System.out.println(bigDecimal1.divideToIntegralValue(bigDecimal2));
n次方
/*
n次方
输出结果:2269042.670616168937363978068644011201
*/
System.out.println(bigDecimal1.pow(10));
小数点
/*
设置小数点的位数
输出结果:4.3210000000
*/
System.out.println(bigDecimal1.setScale(10));
BigInteger
//初始化两个对象
BigInteger bigInteger1 = new BigInteger("123");
BigInteger bigInteger2 = new BigInteger("456");
加法
/*
加法
输出结果:579
*/
System.out.println(bigInteger1.add(bigInteger2));
减法
/*
减法
输出结果:-333
*/
System.out.println(bigInteger1.subtract(bigInteger2));
乘法
/*
乘法
输出结果:56088
*/
System.out.println(bigInteger1.multiply(bigInteger2));
除法
/*
除法
输出结果:0
*/
System.out.println(bigInteger1.divide(bigInteger2));
绝对值
/*
绝对值
输出结果:123
*/
System.out.println(bigInteger1.abs());
负值
/*
负值
输出结果:-123
*/
System.out.println(bigInteger1.negate());
n次方
/*
n次方
输出结果:15129
*/
System.out.println(bigInteger1.pow(2));
除法和取模
/*
取模和除数 返回BigInteger的数组
输出结果:0,123
*/
BigInteger bigIntegers[] = bigInteger1.divideAndRemainder(bigInteger2);
System.out.println(bigIntegers[0] + "," + bigIntegers[1]);
取模
/*
取模
输出结果:123
*/
System.out.println(bigInteger1.remainder(bigInteger2));
and
/*
and
输出结果:72
*/
System.out.println(bigInteger1.and(bigInteger2));
andNot
/*
andNot
输出结果:51
*/
System.out.println(bigInteger1.andNot(bigInteger2));
not
/*
not
输出结果:-124
*/
System.out.println(bigInteger1.not());
or
/*
or
输出结果:507
*/
System.out.println(bigInteger1.or(bigInteger2));
xor
/*
xor 异或
输出结果:435
*/
System.out.println(bigInteger1.xor(bigInteger2));
最大公约数
/*
最大公约数
存在c,可以让a、b都能整除,c叫a、b的公约数
例如:18、6的公约数有:1、2、3、6 最大公因数就是6
输出结果:3
*/
System.out.println(bigInteger1.gcd(bigInteger2));
最小公倍数
/*
最小公倍数
存在c,可以把a、b都整除,c叫a、b的公倍数
例如:18、6的公倍数有:18、36...等等 18就是最小公倍数
因为该类没有自带计算公倍数的函数,需要我们自己利用最大公因数去计算。最小公倍数=(a*b/最大公因数)
输出结果:18696
*/
// 获得最大公约数
BigInteger bigInteger3 = bigInteger1.gcd(bigInteger2);
// 获得(a*b)的结果
BigInteger bigInteger4 = bigInteger1.multiply(bigInteger2);
//获得(a*b/最大公因数)的结果
System.out.println(bigInteger4.divide(bigInteger3));