BigInteger

​ 如果在操作的时候一个整型数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作。

​ BigInteger是在java.math包中。

代码示例:

package ustc.lichunchun.bigdataapi;
import java.math.BigInteger;
public class BigIntegerDemo1 {
	public static void main(String[] args) {
		BigInteger bi1 = new BigInteger("123456789") ;	// 声明BigInteger对象
		BigInteger bi2 = new BigInteger("987654321") ;	// 声明BigInteger对象
		System.out.println("加法操作:" + bi2.add(bi1)) ;	// 加法操作
		System.out.println("减法操作:" + bi2.subtract(bi1)) ;	// 减法操作
		System.out.println("乘法操作:" + bi2.multiply(bi1)) ;	// 乘法操作
		System.out.println("除法操作:" + bi2.divide(bi1)) ;	// 除法操作
		System.out.println("最大数:" + bi2.max(bi1)) ;	 // 求出最大数
		System.out.println("最小数:" + bi2.min(bi1)) ;	 // 求出最小数
		BigInteger result[] = bi2.divideAndRemainder(bi1) ;	// 求出余数的除法操作
		System.out.println("商是:" + result[0] + ";余数是:" + result[1]) ;
	}

}

发现divide()方法本身只是把最终的商保存下来了,但是这样的两个数字相除的时候肯定是无法整除,肯定存在余数,所以我们在上面代码中还用到了divideAndRemainder()方法来获得结果和余数。

BigDecimal

​ 使用此类可以完成大的小数操作,而且也可以使用此类进行精确的四舍五入,这一点在开发中经常使用。
​ 对于不需要任何准确计算精度的程序可以直接使用float或double完成,但是如果需要精确计算结果,则必须使用BigDecimal类。

代码示例:

package ustc.lichunchun.bigdataapi;

import java.math.BigDecimal;
public class BigDecimalDemo01 {
	public static void main(String[] args) {
		System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345,3.333),1)) ;
		System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345,3.333),3)) ;
		System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345,3.333),4)) ;
		System.out.println("除法运算:" + MyMath.div(10.345,3.333,3)) ;
	}
}

class MyMath{
	public static double add(double d1,double d2){		// 进行加法计算
		BigDecimal b1 = new BigDecimal(d1) ;
		BigDecimal b2 = new BigDecimal(d2) ;
		return b1.add(b2).doubleValue() ;
	}
	public static double sub(double d1,double d2){		// 进行减法计算
		BigDecimal b1 = new BigDecimal(d1) ;
		BigDecimal b2 = new BigDecimal(d2) ;
		return b1.subtract(b2).doubleValue() ;
	}
	public static double mul(double d1,double d2){		// 进行乘法计算
		BigDecimal b1 = new BigDecimal(d1) ;
		BigDecimal b2 = new BigDecimal(d2) ;
		return b1.multiply(b2).doubleValue() ;
	}
	public static double div(double d1,double d2,int len){		// 进行除法计算
		BigDecimal b1 = new BigDecimal(d1) ;
		BigDecimal b2 = new BigDecimal(d2) ;
		return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ;
	}

	public static double round(double d,int len){	// 进行四舍五入
		BigDecimal b1 = new BigDecimal(d) ;
		BigDecimal b2 = new BigDecimal(1) ; // 技巧
		return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ;
	}

};
Modifier and Type Method and Description
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()返回大于这个 BigIntegerBigInteger的第一个整数。
BigInteger not()返回值为 (~this)
BigInteger or(BigInteger val)返回值为 `(this
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)