【蓝桥杯】Java组必备API类 --高精大数类 BigInteger BigDecimal总结

本文详细介绍了Java中的大数类BigInteger和BigDecimal,包括它们的构造方法、基本操作、四则运算和其他数学运算。重点强调了使用字符串构造大数以避免精度损失,并提供了丰富的代码示例来展示如何进行算术运算、比较、取余、求最大公约数等操作。此外,还提到了BigDecimal在处理浮点数时的精度问题及其解决方案。

在这里插入图片描述

大数类

大数类一共有两个,分别是BigInteger和BigDecimal,大数整形变量以及大数浮点数,理论上可以存储无线长的数字(只要你计算机的内存足够),接下来我将会分别介绍这两个类。

BigInteger
构造函数

BigInteger一共有六个构造方法,分别是以下六个,个人认为最常用的应该是第五个将字符串转换为BigInteger。

BigInteger b = BigInteger(byte[] val) 
//将包含BigInteger的二进制补码二进制表达式的字节数组转换为BigInteger。  
BigInteger b = BigInteger(int signum, byte[] magnitude) 
//将BigInteger的符号大小表示形式转换为BigInteger。  
BigInteger b = BigInteger(int bitLength, int certainty, Random rnd) 
//构造一个随机生成的正BigInteger,它可能是素数,具有指定的bitLength。  
BigInteger b = BigInteger(int numBits, Random rnd) 
//构造一个随机生成的BigInteger,均匀分布在0到(2 numBits - 1)的范围内。  
BigInteger b = BigInteger(String val) 
//将BigInteger的十进制字符串表示形式转换为BigInteger。  
BigInteger b = BigInteger(String val, int radix) 
//将指定基数中的BigInteger的String表示形式转换为BigInteger。  
输入

BigInteger类是可以直接读入的,当然你也可以选择先读入字符串然后再转到BigInteger。

Scanner s = new Scanner(System.in);
while (sc.hasNextBigInteger()) {
BigInteger b = s.nextBigInteger();  //读取BigInteger
输出

BigInteger类是可以直接输出的

System.out.println(b);
四则运算

BigInteger的四则运算不能用基础的四个符号进行操作,而是需要用BigInteger的方法来实现。

BigInteger b1 = new BigInteger("1000000");
BigInteger b2 = new BigInteger("-2000000");
System.out.println(b2.add(b1));  //加
System.out.println(b2.subtract(b1));  //减
System.out.println(b2.multiply(b1));   //乘
System.out.println(b2.divide(b1));   //除
其他运算
System.out.println(b2.remainder(b1));   //取余
System.out.println(b2.negate());   //取负号
System.out.println(b2.equals(b1));   //判断相等
System.out.println(b2.gcd(b1));   //求最大公约数
System.out.println(b2.max(b1));   //求最大值
System.out.println(b2.min(b1));   //求最小值
System.out.println(b2.pow(100000));   //开方
位运算
System.out.println(b2.and(b1));   //并
System.out.println(b2.not());    //取反
System.out.println(b2.or(b1));   //或
System.out.println(b2.xor(b1));   //异或
方法

BigInteger 的方法如下所示(注意不是静态方法)

BigInteger abs() 
//返回一个BigInteger,它的值是此BigInteger的绝对值。  
BigInteger add(BigInteger val) 
//返回值为 (this + val) 。  
BigInteger and(BigInteger val) 
//返回值为 (this & val) 。  
BigInteger andNot(BigInteger val) 
//返回值为 (this & ~val) 。  
int bitCount() 
//返回与其符号位不同的BigInteger的二进制补码表示中的位数。  
int bitLength() 
//返回此BigInteger的最小二进制补码表示中的位数, 不包括符号位。  
byte byteValueExact() 
//将此 BigInteger转换为 byte ,检查丢失的信息。  
BigInteger clearBit(int n) 
//返回一个BigInteger,其值等于此BigInteger,指定的位被清零。  
int compareTo(BigInteger val) 
//将此BigInteger与指定的BigInteger进行比较。  
BigInteger divide(BigInteger val) 
//返回值为 (this / val) 。  
BigInteger[] divideAndRemainder(BigInteger val) 
//返回两个BigInteger的数组,其中包含 (this / val)后跟 (this % val) 。  
double doubleValue() 
//将此BigInteger转换为 double 。  
boolean equals(Object x) 
//将此BigInteger与指定的对象进行比较以实现相等。  
BigInteger flipBit(int n) 
//返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。  
float floatValue() 
//将此BigInteger转换为 float 。  
BigInteger gcd(BigInteger val) 
//返回一个BigInteger,其值是 abs(this)和 abs(val) 。  
int getLowestSetBit() 
//返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。  
int hashCode() 
//返回此BigInteger的哈希码。  
int intValue() 
//将此BigInteger转换为 int 。  
int intValueExact() 
//将此 BigInteger转换为 int ,检查丢失的信息。  
boolean isProbablePrime(int certainty) 
//返回 true如果这个BigInteger可能是素数, false如果它是绝对复合。  
long longValue() 
//将此BigInteger转换为 long 。  
long longValueExact() 
//将此 BigInteger转换为 long ,检查丢失的信息。  
BigInteger max(BigInteger val) 
//返回此BigInteger和 val 。  
BigInteger min(BigInteger val) 
//返回此BigInteger和 val 。  
BigInteger mod(BigInteger m) 
//返回值为 (this mod m )。  
BigInteger modInverse(BigInteger m) 
//返回值为 (this -1 mod m) 。  
BigInteger modPow(BigInteger exponent, BigInteger m) 
//返回值为 (thisexponent mod m)的BigInteger 。  
BigInteger multiply(BigInteger val) 
//返回值为 (this * val) 。  
BigInteger negate() 
//返回值为 (-this) 。  
BigInteger nextProbablePrime() 
//返回大于这个 BigInteger为 BigInteger的第一个整数。  
BigInteger not() 
//返回值为 (~this) 。  
BigInteger or(BigInteger val) 
//返回值为 (this | val) 。  
BigInteger pow(int exponent) 
//返回值为 (thisexponent)的BigInteger 。  
static BigInteger probablePrime(int bitLength, Random rnd) 
//返回一个正的BigInteger,它可能是素数,具有指定的位长度。  
BigInteger remainder(BigInteger val) 
//返回值为 (this % val) 。  
BigInteger setBit(int n) 
//返回一个BigInteger,其值等于具有指定位集合的BigInteger。  
BigInteger shiftLeft(int n) 
//返回值为 (this << n) 。  
BigInteger shiftRight(int n) 
//返回值为 (this >> n) 。  
short shortValueExact() 
//将此 BigInteger转换为 short ,检查丢失的信息。  
int signum() 
//返回此BigInteger的signum函数。  
BigInteger subtract(BigInteger val) 
//返回值为 (this - val) 。  
boolean testBit(int n) 
//返回 true当且仅当指定的位被设置。  
byte[] toByteArray() 
//返回一个包含此BigInteger的二进制补码表示的字节数组。  
String toString() 
//返回此BigInteger的十进制字符串表示形式。  
String toString(int radix) 
//返回给定基数中BigInteger的String表示形式。  
static BigInteger valueOf(long val) 
//返回一个BigInteger,其值等于指定的 long 。  
BigInteger xor(BigInteger val) 
//返回值为 (this ^ val) 。  
BigDecimal
构造函数

BigDecimal一共有十六个构造方法,分别是以下十六个,同样最常用的方法我认为还是传入字符串

BigDecimal(BigInteger val) 
//将 BigInteger转换成 BigDecimal 。  
BigDecimal(BigInteger unscaledVal, int scale) 
//将BigInteger的 BigInteger值和 int等级转换为 BigDecimal 。  
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) 
//将 BigInteger未缩放值和 int扩展转换为 BigDecimal ,根据上下文设置进行舍入。  
BigDecimal(BigInteger val, MathContext mc) 
//根据上下文设置将 BigInteger转换为 BigDecimal舍入。  
BigDecimal(char[] in) 
//一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造。  
BigDecimal(char[] in, int offset, int len) 
//一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造,同时允许一个子阵列被指定。  
BigDecimal(char[] in, int offset, int len, MathContext mc) 
//一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受字符作为的相同序列 BigDecimal(String)构造,同时允许指定一个子阵列和用根据上下文设置进行舍入。  
BigDecimal(char[] in, MathContext mc) 
//一个转换的字符数组表示 BigDecimal成 BigDecimal ,接受相同的字符序列作为 BigDecimal(String)构造与根据上下文设置进行舍入。 
BigDecimal(double val) 
//将 double转换为 BigDecimal ,这是 double的二进制浮点值的精确十进制表示。  
BigDecimal(double val, MathContext mc) 
//将 double转换为 BigDecimal ,根据上下文设置进行舍入。  
BigDecimal(int val) 
//将 int成 BigDecimal 。  
BigDecimal(int val, MathContext mc) 
//将 int转换为 BigDecimal ,根据上下文设置进行舍入。  
BigDecimal(long val) 
//将 long成 BigDecimal 。  
BigDecimal(long val, MathContext mc) 
//将 long转换为 BigDecimal ,根据上下文设置进行舍入。  
BigDecimal(String val) 
//将BigDecimal的字符串表示 BigDecimal转换为 BigDecimal 。  
BigDecimal(String val, MathContext mc) 
//一个转换的字符串表示 BigDecimal成 BigDecimal ,接受相同的字符串作为 BigDecimal(String)构造,利用根据上下文设置进行舍入。 

注意:

上面的那些是我从JavaAPI上面得来的,但是以下这一点一定要注意!

最好使用String来传参构造,而不用double,原因如下

请看下列代码

BigDecimal b1 =new BigDecimal(0.1);
System.out.println(b1);
BigDecimal b2 =new BigDecimal("0.1");
System.out.println(b2);

最后的结果为:

在这里插入图片描述

为什么会这样呢?

double构造的结果具有一定的不可预知性,因为由于二进制的原因0.1并没有办法很好的表示,二传入String的构造结果是完全可信的,也就是会正好的得到想要的0.1,所以我们构造BigDecimal的值时最好使用String来构造。

输入

BigDecimal类是可以直接读入的,当然你也可以选择先读入字符串然后再转到BigDecimal。

Scanner s = new Scanner(System.in);
while (sc.hasNextBigDecimal()) {
	BigDecimal b = s.nextBigDecimal();  //读取BigInteger
}
输出

BigDecimalr类是可以直接输出的

System.out.println(b);
四则运算

BigDecimal的四则运算不能用基础的四个符号进行操作,而是需要用BigDecimal的方法来实现。

BigDecimal b1 =new BigDecimal(0.1);
BigDecimal b2 =new BigDecimal("0.2");
System.out.println(b2.add(b1));  //加
System.out.println(b2.subtract(b1));  //减
System.out.println(b2.multiply(b1));   //乘
System.out.println(b2.divide(b1));   //除
其他运算
System.out.println(b2.remainder(b1));   //取余
System.out.println(b2.negate());   //取负号
System.out.println(b2.equals(b1));   //判断相等
System.out.println(b2.compareTo(b1));  //比大小(比得过是1,相等是0,比不过是-1)
System.out.println(b2.gcd(b1));   //求最大公约数
System.out.println(b2.max(b1));   //求最大值
System.out.println(b2.min(b1));   //求最小值
System.out.println(b2.pow(100000));   //开方
方法

BigDecimal的方法如下所示(注意不是静态方法)

BigDecimal abs() 
//返回一个 BigDecimal ,其值为此 BigDecimal的绝对值,其缩放比例为 this.scale() 。  
BigDecimal abs(MathContext mc) 
//返回一个 BigDecimal ,其值为此 BigDecimal的绝对值,根据上下文设置进行舍入。  
BigDecimal add(BigDecimal augend) 
//返回 BigDecimal ,其值是 (this + augend) ,其标为 max(this.scale(), augend.scale()) 。  
BigDecimal add(BigDecimal augend, MathContext mc) 
//返回 BigDecimal ,其值是 (this + augend) ,根据上下文设置进行舍入。  
byte byteValueExact() 
//将此 BigDecimal转换为 byte ,检查丢失的信息。  
int compareTo(BigDecimal val) 
//将此 BigDecimal与指定的BigDecimal进行 BigDecimal 。  
BigDecimal divide(BigDecimal divisor) 
//返回BigDecimal ,其值为(this / divisor) ,优先级为(this.scale() - divisor.scale()) ; 如果不能表示确切的商(因为它具有非终止的十进制扩展),则抛出一个ArithmeticException 。  
BigDecimal divide(BigDecimal divisor, int roundingMode) 
//返回 BigDecimal ,其值是 (this / divisor) ,其标为 this.scale() 。  
BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) 
//返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。  
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 
//返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。  
BigDecimal divide(BigDecimal divisor, MathContext mc) 
//返回 BigDecimal ,其值是 (this / divisor) ,根据上下文设置进行舍入。  
BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) 
//返回 BigDecimal ,其值是 (this / divisor) ,其标为 this.scale() 。  
BigDecimal[] divideAndRemainder(BigDecimal divisor) 
//返回一个两元件 BigDecimal阵列含有的结果 divideToIntegralValue接着的结果 remainder上的两个操作数。  
BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) 
//返回一个两元件 BigDecimal阵列含有的结果 divideToIntegralValue接着的结果 remainder上与根据上下文设置进行舍入计算出的两个操作数。  
BigDecimal divideToIntegralValue(BigDecimal divisor) 
//返回一个 BigDecimal ,它的值是 BigDecimal的整数部分 (this / divisor)取整。  
BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) 
//返回值为 BigDecimal的整数部分的 (this / divisor) 。  
double doubleValue() 
//将此 BigDecimal转换为 double 。  
boolean equals(Object x) 
//将此 BigDecimal与指定的 Object进行比较以获得相等性。  
float floatValue() 
//将此 BigDecimal转换为 float 。  
int hashCode() 
//返回此 BigDecimal的哈希码。  
int intValue() 
//将此 BigDecimal转换为 int 。  
int intValueExact() 
//将此 BigDecimal转换为 int ,检查丢失的信息。  
long longValue() 
//将此 BigDecimal转换为 long 。  
long longValueExact() 
//将此 BigDecimal转换为 long ,检查丢失的信息。  
BigDecimal max(BigDecimal val) 
//返回此 BigDecimal和 val 。  
BigDecimal min(BigDecimal val) 
//返回此 BigDecimal和 val 。  
BigDecimal movePointLeft(int n) 
//返回一个 BigDecimal ,相当于这个小数点,向左移动了 n个地方。  
BigDecimal movePointRight(int n) 
//返回一个 BigDecimal ,相当于这个小数点移动了 n个地方。  
BigDecimal multiply(BigDecimal multiplicand) 
//返回 BigDecimal ,其值是 (this × multiplicand),其标为 (this.scale() + multiplicand.scale()) 。  
BigDecimal multiply(BigDecimal multiplicand, MathContext mc) 
//返回 BigDecimal ,其值是 (this × multiplicand),根据上下文设置进行舍入。  
BigDecimal negate() 
//返回 BigDecimal ,其值是 (-this) ,其标为 this.scale() 。  
BigDecimal negate(MathContext mc) 
//返回 BigDecimal ,其值是 (-this) ,根据上下文设置进行舍入。  
BigDecimal plus() 
//返回 BigDecimal ,其值是 (+this) ,其标为 this.scale() 。  
BigDecimal plus(MathContext mc) 
//返回 BigDecimal ,其值是 (+this) ,根据上下文设置进行舍入。  
BigDecimal pow(int n) 
//返回 BigDecimal ,其值是 (thisn),该电源,准确计算,使其具有无限精度。  
BigDecimal pow(int n, MathContext mc) 
//返回 BigDecimal ,其值是 (thisn)。  
int precision() 
//返回此 BigDecimal的 BigDecimal 。  
BigDecimal remainder(BigDecimal divisor) 
//返回 BigDecimal ,其值是 (this % divisor) 。  
BigDecimal remainder(BigDecimal divisor, MathContext mc) 
//返回 BigDecimal ,其值是 (this % divisor) ,根据上下文设置进行舍入。  
BigDecimal round(MathContext mc) 
//返回 BigDecimal根据四舍五入 MathContext设置。  
int scale() 
//返回此 规模 BigDecimal 。  
BigDecimal scaleByPowerOfTen(int n) 
//返回一个BigDecimal,其数值等于( this * 10 n )。  
BigDecimal setScale(int newScale) 
//返回一个 BigDecimal ,其大小是指定值,其值在数字上等于此 BigDecimal 。  
BigDecimal setScale(int newScale, int roundingMode) 
//返回一个 BigDecimal ,其规模是指定值,其缩放值通过将此 BigDecimal的非标度值乘以10的适当功率来确定,以维持其总体值。  
BigDecimal setScale(int newScale, RoundingMode roundingMode) 
//返回一个 BigDecimal ,其规模是指定值,其缩放值通过将该 BigDecimal的非标度值乘以10的适当功率来确定,以维持其整体值。  
short shortValueExact() 
//将此 BigDecimal转换为 short ,检查丢失的信息。  
int signum() 
//返回这个 BigDecimal的signum函数。  
BigDecimal stripTrailingZeros() 
//返回一个 BigDecimal ,它在数字上等于此值, BigDecimal表示中删除任何尾随的零。  
BigDecimal subtract(BigDecimal subtrahend) 
//返回 BigDecimal ,其值是 (this - subtrahend) ,其标为 max(this.scale(), subtrahend.scale()) 。  
BigDecimal subtract(BigDecimal subtrahend, MathContext mc) 
//返回 BigDecimal ,其值是 (this - subtrahend) ,根据上下文设置进行舍入。  
BigInteger toBigInteger() 
//将此 BigDecimal转换为 BigInteger 。  
BigInteger toBigIntegerExact() 
//将此 BigDecimal转换为 BigInteger ,检查丢失的信息。  
String toEngineeringString() 
//如果需要指数,则使用工程符号返回此 BigDecimal的字符串表示形式。  
String toPlainString() 
//返回没有指数字段的此 BigDecimal的字符串表示形式。  
String toString() 
//返回此 BigDecimal的字符串表示,如果需要指数,则使用科学计数法。  
BigDecimal ulp() 
//返回此 BigDecimal的最后一个位置的ulp(一个单位)的大小。  
BigInteger unscaledValue() 
//返回一个 BigInteger ,其值是此 BigDecimal的 未缩放值 。  
static BigDecimal valueOf(double val) 
//转换一个 double成 BigDecimal ,使用 double通过所提供的规范的字符串表示 Double.toString(double)方法。  
static BigDecimal valueOf(long val) 
//将 long值转换为 BigDecimal ,比例为零。  
static BigDecimal valueOf(long unscaledVal, int scale) 
//将 long值和 int比例转换为 BigDecimal 。  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值