0
点赞
收藏
分享

微信扫一扫

java基础之大数值(BigInteger和BigDecimal)

夕颜合欢落 2022-05-05 阅读 82
java

当基本的整数和浮点数不能够满足需求时,那么可以使用java.math包中的两个很有用的类:BigIntegerBigDecimal。这两个类可以处理包含任意长度数字的序列的数值。BigInteger类实现了任意精度的整数运算,BigDecimal实现了任意精度的浮点数运算。

一、BigInteger

public class BigInteger extends Number implements Comparable<BigInteger>

1.1、简介

不可变的任意精度的整数。所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。当为任何输入参数传递 null 对象引用时,此类中的所有方法和构造方法都将抛出 NullPointerException

1.2、构造方法

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

1.3、常用方法

返回类型方法
 BigIntegerabs()
          返回其值是此 BigInteger 的绝对值的 BigInteger。
 BigIntegeradd(BigInteger val)
          返回其值为 (this + val) 的 BigInteger。
 BigIntegerand(BigInteger val)
          返回其值为 (this & val) 的 BigInteger。
 BigIntegerandNot(BigInteger val)
          返回其值为 (this & ~val) 的 BigInteger。
 intbitCount()
          返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。
 intbitLength()
          返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。
 BigIntegerclearBit(int n)
          返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。
 intcompareTo(BigInteger val)
          将此 BigInteger 与指定的 BigInteger 进行比较。
 BigIntegerdivide(BigInteger val)
          返回其值为 (this / val) 的 BigInteger。
 BigInteger[]divideAndRemainder(BigInteger val)
          返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
 doubledoubleValue()
          将此 BigInteger 转换为 double
 booleanequals(Object x)
          比较此 BigInteger 与指定的 Object 的相等性。
 BigIntegerflipBit(int n)
          返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。
 floatfloatValue()
          将此 BigInteger 转换为 float
 BigIntegergcd(BigInteger val)
          返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。
 intgetLowestSetBit()
          返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。
 inthashCode()
          返回此 BigInteger 的哈希码。
 intintValue()
          将此 BigInteger 转换为 int
 booleanisProbablePrime(int certainty)
          如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。
 longlongValue()
          将此 BigInteger 转换为 long
 BigIntegermax(BigInteger val)
          返回此 BigInteger 和 val 的最大值。
 BigIntegermin(BigInteger val)
          返回此 BigInteger 和 val 的最小值。
 BigIntegermod(BigInteger m)
          返回其值为 (this mod m) 的 BigInteger。
 BigIntegermodInverse(BigInteger m)
          返回其值为 (this-1 mod m) 的 BigInteger。
 BigIntegermodPow(BigInteger exponent, BigInteger m)
          返回其值为 (thisexponent mod m) 的 BigInteger。
 BigIntegermultiply(BigInteger val)
          返回其值为 (this * val) 的 BigInteger。
 BigIntegernegate()
          返回其值是 (-this) 的 BigInteger。
 BigIntegernextProbablePrime()
          返回大于此 BigInteger 的可能为素数的第一个整数。
 BigIntegernot()
          返回其值为 (~this) 的 BigInteger。
 BigIntegeror(BigInteger val)
          返回其值为 (this | val) 的 BigInteger。
 BigIntegerpow(int exponent)
          返回其值为 (thisexponent) 的 BigInteger。
static BigIntegerprobablePrime(int bitLength, Random rnd)
          返回有可能是素数的、具有指定长度的正 BigInteger。
 BigIntegerremainder(BigInteger val)
          返回其值为 (this % val) 的 BigInteger。
 BigIntegersetBit(int n)
          返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。
 BigIntegershiftLeft(int n)
          返回其值为 (this << n) 的 BigInteger。
 BigIntegershiftRight(int n)
          返回其值为 (this >> n) 的 BigInteger。
 intsignum()
          返回此 BigInteger 的正负号函数。
 BigIntegersubtract(BigInteger val)
          返回其值为 (this - val) 的 BigInteger。
 booleantestBit(int n)
          当且仅当设置了指定的位时,返回 true。
 byte[]toByteArray()
          返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。
 StringtoString()
          返回此 BigInteger 的十进制字符串表示形式。
 StringtoString(int radix)
          返回此 BigInteger 的给定基数的字符串表示形式。
static BigIntegervalueOf(long val)
          返回其值等于指定 long 的值的 BigInteger。
 BigIntegerxor(BigInteger val)
          返回其值为 (this ^ val) 的 BigInteger。

二、BigDecimal

public class BigDecimal extends Number implements Comparable<BigDecimal>

2.1、简介

不可变的、任意精度的有符号十进制数。BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成。如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以 10 的负 scale 次幂。因此,BigDecimal 表示的数值是 (unscaledValue × 10-scale)。

BigDecimal 类提供以下操作:算术、标度操作、舍入、比较、哈希算法和格式转换。toString()方法提供 BigDecimal 的规范表示形式。

2.2、静态常量

字段摘要
static BigDecimalONE
          值为 1,标度为 0。
static intROUND_CEILING
          接近正无穷大的舍入模式。
static intROUND_DOWN
          接近零的舍入模式。
static intROUND_FLOOR
          接近负无穷大的舍入模式。
static intROUND_HALF_DOWN
          向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。
static intROUND_HALF_EVEN
          向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
static intROUND_HALF_UP
          向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。
static intROUND_UNNECESSARY
          断言请求的操作具有精确的结果,因此不需要舍入。
static intROUND_UP
          舍入远离零的舍入模式。
static BigDecimalTEN
          值为 10,标度为 0。
static BigDecimalZERO
          值为 0,标度为 0。

2.3、构造方法

BigDecimal(BigInteger val)
          将 BigInteger 转换为 BigDecimal。
BigDecimal(BigInteger unscaledVal, int scale)
          将 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(String val, MathContext mc)
          将 BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符串(按照上下文设置进行舍入)。

2.4、方法

返回类型方法
 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)
          返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。
 bytebyteValueExact()
          将此 BigDecimal 转换为 byte,以检查丢失的信息。
 intcompareTo(BigDecimal val)
          将此 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)
          返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。
 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,其值为向下舍入所得商值 (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)
          返回其值为 (this × multiplicand) 的 BigDecimal(根据上下文设置进行舍入)。
 BigDecimalnegate()
          返回 BigDecimal,其值为 (-this),其标度为 this.scale()。
 BigDecimalnegate(MathContext mc)
          返回其值为 (-this) 的 BigDecimal(根据上下文设置进行舍入)。
 BigDecimalplus()
          返回 BigDecimal,其值为 (+this),其标度为 this.scale()。
 BigDecimalplus(MathContext mc)
          返回其值为 (+this) 的 BigDecimal(根据上下文设置进行舍入)。
 BigDecimalpow(int n)
          返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。
 BigDecimalpow(int n, MathContext mc)
          返回其值为 (thisn) 的 BigDecimal。
 intprecision()
          返回此 BigDecimal 的精度
 BigDecimalremainder(BigDecimal divisor)
          返回其值为 (this % divisor) 的 BigDecimal。
 BigDecimalremainder(BigDecimal divisor, MathContext mc)
          返回其值为 (this % divisor) 的 BigDecimal(根据上下文设置进行舍入)。
 BigDecimalround(MathContext mc)
          返回根据 MathContext 设置进行舍入后的 BigDecimal。
 intscale()
          返回此 BigDecimal 的标度
 BigDecimalscaleByPowerOfTen(int n)
          返回其数值等于 (this * 10n) 的 BigDecimal。
 BigDecimalsetScale(int newScale)
          返回一个 BigDecimal,其标度为指定值,其值在数值上等于此 BigDecimal 的值。
 BigDecimalsetScale(int newScale, int roundingMode)
          返回一个 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
 BigDecimalsetScale(int newScale, RoundingMode roundingMode)
          返回 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
 shortshortValueExact()
          将此 BigDecimal 转换为 short,以检查丢失的信息。
 intsignum()
          返回此 BigDecimal 的正负号函数。
 BigDecimalstripTrailingZeros()
          返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal。
 BigDecimalsubtract(BigDecimal subtrahend)
          返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
 BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)
          返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。
 BigIntegertoBigInteger()
          将此 BigDecimal 转换为 BigInteger。
 BigIntegertoBigIntegerExact()
          将此 BigDecimal 转换为 BigInteger,以检查丢失的信息。
 StringtoEngineeringString()
          返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。
 StringtoPlainString()
          返回不带指数字段的此 BigDecimal 的字符串表示形式。
 StringtoString()
          返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。
 BigDecimalulp()
          返回此 BigDecimal 的 ulp(最后一位的单位)的大小。
 BigIntegerunscaledValue()
          返回其值为此 BigDecimal 的非标度值 的 BigInteger。
static BigDecimalvalueOf(double val)
          使用 Double.toString(double) 方法提供的 double 规范的字符串表示形式将 double 转换为 BigDecimal。
static BigDecimalvalueOf(long val)
          将 long 值转换为具有零标度的 BigDecimal。
static BigDecimalvalueOf(long unscaledVal, int scale)
          将 long 非标度值和 int 标度转换为 BigDecimal。
举报

相关推荐

0 条评论