java中大数的计算BigInteger和BigDecimal两个类的常用方法

1、BigInteger

Modifier and TypeMethod 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()
返回大于这个 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) 。

2、BigDecimal

Modifier and TypeMethod and Description
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 。
原文地址:https://www.cnblogs.com/torchstar/p/15152490.html