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

79 阅读7分钟

1、BigInteger

Modifier and TypeMethod and Description
BigIntegerabs()返回一个BigInteger,它的值是此BigInteger的绝对值。
BigIntegeradd(BigInteger val)返回值为 (this + val) 。
BigIntegerand(BigInteger val)返回值为 (this & val) 。
BigIntegerandNot(BigInteger val)返回值为 (this & ~val) 。
intbitCount()返回与其符号位不同的BigInteger的二进制补码表示中的位数。
intbitLength()返回此BigInteger的最小二进制补码表示中的位数, 包括符号位。
bytebyteValueExact()将此 BigInteger转换为 byte ,检查丢失的信息。
BigIntegerclearBit(int n)返回一个BigInteger,其值等于此BigInteger,指定的位被清零。
intcompareTo(BigInteger val)将此BigInteger与指定的BigInteger进行比较。
BigIntegerdivide(BigInteger val)返回值为 (this / val) 。
BigInteger[]divideAndRemainder(BigInteger val)返回两个BigInteger的数组,其中包含 (this / val)后跟 (this % val) 。
doubledoubleValue()将此BigInteger转换为 double 。
booleanequals(Object x)将此BigInteger与指定的对象进行比较以实现相等。
BigIntegerflipBit(int n)返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。
floatfloatValue()将此BigInteger转换为 float 。
BigIntegergcd(BigInteger val)返回一个BigInteger,其值是 abs(this)和 abs(val) 。
intgetLowestSetBit()返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。
inthashCode()返回此BigInteger的哈希码。
intintValue()将此BigInteger转换为 int 。
intintValueExact()将此 BigInteger转换为 int ,检查丢失的信息。
booleanisProbablePrime(int certainty)返回 true如果这个BigInteger可能是素数, false如果它是绝对复合。
longlongValue()将此BigInteger转换为 long 。
longlongValueExact()将此 BigInteger转换为 long ,检查丢失的信息。
BigIntegermax(BigInteger val)返回此BigInteger和 val 。
BigIntegermin(BigInteger val)返回此BigInteger和 val 。
BigIntegermod(BigInteger m)返回值为 (this mod m )。
BigIntegermodInverse(BigInteger m)返回值为 (this -1 mod m) 。
BigIntegermodPow(BigInteger exponent, BigInteger m)返回值为 (thisexponent mod m)的BigInteger 。
BigIntegermultiply(BigInteger val)返回值为 (this * val) 。
BigIntegernegate()返回值为 (-this) 。
BigIntegernextProbablePrime()返回大于这个 BigInteger为 BigInteger的第一个整数。
BigIntegernot()返回值为 (~this) 。
BigIntegeror(BigInteger val)返回值为 (this | val) 。
BigIntegerpow(int exponent)返回值为 (thisexponent)的BigInteger 。
static BigIntegerprobablePrime(int bitLength, Random rnd)返回一个正的BigInteger,它可能是素数,具有指定的位长度。
BigIntegerremainder(BigInteger val)返回值为 (this % val) 。
BigIntegersetBit(int n)返回一个BigInteger,其值等于具有指定位集合的BigInteger。
BigIntegershiftLeft(int n)返回值为 (this << n) 。
BigIntegershiftRight(int n)返回值为 (this >> n) 。
shortshortValueExact()将此 BigInteger转换为 short ,检查丢失的信息。
intsignum()返回此BigInteger的signum函数。
BigIntegersubtract(BigInteger val)返回值为 (this - val) 。
booleantestBit(int n)返回 true当且仅当指定的位被设置。
byte[]toByteArray()返回一个包含此BigInteger的二进制补码表示的字节数组。
StringtoString()返回此BigInteger的十进制字符串表示形式。
StringtoString(int radix)返回给定基数中BigInteger的String表示形式。
static BigIntegervalueOf(long val)返回一个BigInteger,其值等于指定的 long 。
BigIntegerxor(BigInteger val)返回值为 (this ^ val) 。

2、BigDecimal

Modifier and TypeMethod and Description
BigDecimalabs()返回一个 BigDecimal ,其值为此 BigDecimal的绝对值,其缩放比例为 this.scale() 。
BigDecimalabs(MathContext mc)返回一个 BigDecimal ,其值为此 BigDecimal的绝对值,根据上下文设置进行舍入。
BigDecimaladd(BigDecimal augend)返回 BigDecimal ,其值是 (this + augend) ,其标为 max(this.scale(), augend.scale()) 。
BigDecimaladd(BigDecimal augend, MathContext mc)返回 BigDecimal ,其值是 (this + augend) ,根据上下文设置进行舍入。
bytebyteValueExact()将此 BigDecimal转换为 byte ,检查丢失的信息。
intcompareTo(BigDecimal val)将此 BigDecimal与指定的BigDecimal进行 BigDecimal 。
BigDecimaldivide(BigDecimal divisor)返回BigDecimal ,其值为(this / divisor) ,优先级为(this.scale() - divisor.scale()) ; 如果不能表示确切的商(因为它具有非终止的十进制扩展),则抛出一个ArithmeticException 。
BigDecimaldivide(BigDecimal divisor, int roundingMode)返回 BigDecimal ,其值是 (this / divisor) ,其标为 this.scale() 。
BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode)返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。
BigDecimaldivide(BigDecimal divisor, int scale, RoundingMode roundingMode)返回一个 BigDecimal ,其值为 (this / divisor) ,其比例为指定。
BigDecimaldivide(BigDecimal divisor, MathContext mc)返回 BigDecimal ,其值是 (this / divisor) ,根据上下文设置进行舍入。
BigDecimaldivide(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上与根据上下文设置进行舍入计算出的两个操作数。
BigDecimaldivideToIntegralValue(BigDecimal divisor)返回一个 BigDecimal ,它的值是 BigDecimal的整数部分 (this / divisor)取整。
BigDecimaldivideToIntegralValue(BigDecimal divisor, MathContext mc)返回值为 BigDecimal的整数部分的 (this / divisor) 。
doubledoubleValue()将此 BigDecimal转换为 double 。
booleanequals(Object x)将此 BigDecimal与指定的 Object进行比较以获得相等性。
floatfloatValue()将此 BigDecimal转换为 float 。
inthashCode()返回此 BigDecimal的哈希码。
intintValue()将此 BigDecimal转换为 int 。
intintValueExact()将此 BigDecimal转换为 int ,检查丢失的信息。
longlongValue()将此 BigDecimal转换为 long 。
longlongValueExact()将此 BigDecimal转换为 long ,检查丢失的信息。
BigDecimalmax(BigDecimal val)返回此 BigDecimal和 val 。
BigDecimalmin(BigDecimal val)返回此 BigDecimal和 val 。
BigDecimalmovePointLeft(int n)返回一个 BigDecimal ,相当于这个小数点,向左移动了 n个地方。
BigDecimalmovePointRight(int n)返回一个 BigDecimal ,相当于这个小数点移动了 n个地方。
BigDecimalmultiply(BigDecimal multiplicand)返回 BigDecimal ,其值是 (this × multiplicand),其标为 (this.scale() + multiplicand.scale()) 。
BigDecimalmultiply(BigDecimal multiplicand, MathContext mc)返回 BigDecimal ,其值是 (this × multiplicand),根据上下文设置进行舍入。
BigDecimalnegate()返回 BigDecimal ,其值是 (-this) ,其标为 this.scale() 。
BigDecimalnegate(MathContext mc)返回 BigDecimal ,其值是 (-this) ,根据上下文设置进行舍入。
BigDecimalplus()返回 BigDecimal ,其值是 (+this) ,其标为 this.scale() 。
BigDecimalplus(MathContext mc)返回 BigDecimal ,其值是 (+this) ,根据上下文设置进行舍入。
BigDecimalpow(int n)返回 BigDecimal ,其值是 (thisn),该电源,准确计算,使其具有无限精度。
BigDecimalpow(int n, MathContext mc)返回 BigDecimal ,其值是 (thisn)。
intprecision()返回此 BigDecimal的 BigDecimal 。
BigDecimalremainder(BigDecimal divisor)返回 BigDecimal ,其值是 (this % divisor) 。
BigDecimalremainder(BigDecimal divisor, MathContext mc)返回 BigDecimal ,其值是 (this % divisor) ,根据上下文设置进行舍入。
BigDecimalround(MathContext mc)返回 BigDecimal根据四舍五入 MathContext设置。
intscale()返回此 规模 BigDecimal 。
BigDecimalscaleByPowerOfTen(int n)返回一个BigDecimal,其数值等于( this * 10 n )。
BigDecimalsetScale(int newScale)返回一个 BigDecimal ,其大小是指定值,其值在数字上等于此 BigDecimal 。
BigDecimalsetScale(int newScale, int roundingMode)返回一个 BigDecimal ,其规模是指定值,其缩放值通过将此 BigDecimal的非标度值乘以10的适当功率来确定,以维持其总体值。
BigDecimalsetScale(int newScale, RoundingMode roundingMode)返回一个 BigDecimal ,其规模是指定值,其缩放值通过将该 BigDecimal的非标度值乘以10的适当功率来确定,以维持其整体值。
shortshortValueExact()将此 BigDecimal转换为 short ,检查丢失的信息。
intsignum()返回这个 BigDecimal的signum函数。
BigDecimalstripTrailingZeros()返回一个 BigDecimal ,它在数字上等于此值, BigDecimal表示中删除任何尾随的零。
BigDecimalsubtract(BigDecimal subtrahend)返回 BigDecimal ,其值是 (this - subtrahend) ,其标为 max(this.scale(), subtrahend.scale()) 。
BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)返回 BigDecimal ,其值是 (this - subtrahend) ,根据上下文设置进行舍入。
BigIntegertoBigInteger()将此 BigDecimal转换为 BigInteger 。
BigIntegertoBigIntegerExact()将此 BigDecimal转换为 BigInteger ,检查丢失的信息。
StringtoEngineeringString()如果需要指数,则使用工程符号返回此 BigDecimal的字符串表示形式。
StringtoPlainString()返回没有指数字段的此 BigDecimal的字符串表示形式。
StringtoString()返回此 BigDecimal的字符串表示,如果需要指数,则使用科学计数法。
BigDecimalulp()返回此 BigDecimal的最后一个位置的ulp(一个单位)的大小。
BigIntegerunscaledValue()返回一个 BigInteger ,其值是此 BigDecimal的 未缩放值 。
static BigDecimalvalueOf(double val)转换一个 double成 BigDecimal ,使用 double通过所提供的规范的字符串表示 Double.toString(double)方法。
static BigDecimalvalueOf(long val)将 long值转换为 BigDecimal ,比例为零。
static BigDecimalvalueOf(long unscaledVal, int scale)将 long值和 int比例转换为 BigDecimal 。