1. 程式人生 > >高精度運算元值 BigDecimal類和BinInteger類

高精度運算元值 BigDecimal類和BinInteger類

BigDecimal類

  1、靜態欄位
  

static BigDecimal ONE 
          值為 1,標度為 0static int ROUND_CEILING 
          接近正無窮大的舍入模式。 
static int ROUND_DOWN 
          接近零的舍入模式。 
static int ROUND_FLOOR 
          接近負無窮大的舍入模式。 
static int ROUND_HALF_DOWN 
          向“最接近的”數字舍入,如果與兩個相鄰數字的距離相等,則為上舍入的舍入模式。 
static
int ROUND_HALF_EVEN 向“最接近的”數字舍入,如果與兩個相鄰數字的距離相等,則向相鄰的偶數舍入。 static int ROUND_HALF_UP 向“最接近的”數字舍入,如果與兩個相鄰數字的距離相等,則為向上舍入的舍入模式。 static int ROUND_UNNECESSARY 斷言請求的操作具有精確的結果,因此不需要舍入。 static int ROUND_UP 舍入遠離零的舍入模式。 static BigDecimal TEN 值為 10,標度為 0static
BigDecimal ZERO 值為 0,標度為 0

 2、構造方法
 

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) 構造方法相同的字串(按照上下文設定進行舍入)。

3、方法

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) 
          返回其值為 (this + augend) 的 BigDecimal(根據上下文設定進行舍入)。 
 byte byteValueExact() 
          將此 BigDecimal 轉換為 byte,以檢查丟失的資訊。 
 int compareTo(BigDecimal val) 
          將此 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) 
          返回其值為 (this / divisor) 的 BigDecimal(根據上下文設定進行舍入)。 
 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,其值為向下舍入所得商值 (this / divisor) 的整數部分。 
 BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) 
          返回 BigDecimal,其值為 (this / divisor) 的整數部分。 
 double doubleValue() 
          將此 BigDecimal 轉換為 double。 
 boolean equals(Object x) 
          比較此 BigDecimal 與指定的 Object 的相等性。 
 float floatValue() 
          將此 BigDecimal 轉換為 floatint hashCode() 
          返回此 BigDecimal 的雜湊碼。 
 int intValue() 
          將此 BigDecimal 轉換為 intint intValueExact() 
          將此 BigDecimal 轉換為 int,以檢查丟失的資訊。 
 long longValue() 
          將此 BigDecimal 轉換為 longlong 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) 
          返回其值為 (this × multiplicand) 的 BigDecimal(根據上下文設定進行舍入)。 
 BigDecimal negate() 
          返回 BigDecimal,其值為 (-this),其標度為 this.scale()。 
 BigDecimal negate(MathContext mc) 
          返回其值為 (-this) 的 BigDecimal(根據上下文設定進行舍入)。 
 BigDecimal plus() 
          返回 BigDecimal,其值為 (+this),其標度為 this.scale()。 
 BigDecimal plus(MathContext mc) 
          返回其值為 (+this) 的 BigDecimal(根據上下文設定進行舍入)。 
 BigDecimal pow(int n) 
          返回其值為 (thisn) 的 BigDecimal,準確計算該冪,使其具有無限精度。 
 BigDecimal pow(int n, MathContext mc) 
          返回其值為 (thisn) 的 BigDecimal。 
 int precision() 
          返回此 BigDecimal 的精度。 
 BigDecimal remainder(BigDecimal divisor) 
          返回其值為 (this % divisor) 的 BigDecimal。 
 BigDecimal remainder(BigDecimal divisor, MathContext mc) 
          返回其值為 (this % divisor) 的 BigDecimal(根據上下文設定進行舍入)。 
 BigDecimal round(MathContext mc) 
          返回根據 MathContext 設定進行舍入後的 BigDecimal。 
 int scale() 
          返回此 BigDecimal 的標度。 
 BigDecimal scaleByPowerOfTen(int n) 
          返回其數值等於 (this * 10n) 的 BigDecimal。 
 BigDecimal setScale(int newScale) 
          返回一個 BigDecimal,其標度為指定值,其值在數值上等於此 BigDecimal 的值。 
 BigDecimal setScale(int newScale, int roundingMode) 
          返回一個 BigDecimal,其標度為指定值,其非標度值通過此 BigDecimal 的非標度值乘以或除以十的適當次冪來確定,以維護其總值。 
 BigDecimal setScale(int newScale, RoundingMode roundingMode) 
          返回 BigDecimal,其標度為指定值,其非標度值通過此 BigDecimal 的非標度值乘以或除以十的適當次冪來確定,以維護其總值。 
 short shortValueExact() 
          將此 BigDecimal 轉換為 short,以檢查丟失的資訊。 
 int signum() 
          返回此 BigDecimal 的正負號函式。 
 BigDecimal stripTrailingZeros() 
          返回數值上等於此小數,但從該表示形式移除所有尾部零的 BigDecimal。 
 BigDecimal subtract(BigDecimal subtrahend) 
          返回一個 BigDecimal,其值為 (this - subtrahend),其標度為 max(this.scale(), subtrahend.scale())。 
 BigDecimal subtract(BigDecimal subtrahend, MathContext mc) 
          返回其值為 (this - subtrahend) 的 BigDecimal(根據上下文設定進行舍入)。 
 BigInteger toBigInteger() 
          將此 BigDecimal 轉換為 BigInteger。 
 BigInteger toBigIntegerExact() 
          將此 BigDecimal 轉換為 BigInteger,以檢查丟失的資訊。 
 String toEngineeringString() 
          返回此 BigDecimal 的字串表示形式,需要指數時,則使用工程計數法。 
 String toPlainString() 
          返回不帶指數字段的此 BigDecimal 的字串表示形式。 
 String toString() 
          返回此 BigDecimal 的字串表示形式,如果需要指數,則使用科學記數法。 
 BigDecimal ulp() 
          返回此 BigDecimal 的 ulp(最後一位的單位)的大小。 
 BigInteger unscaledValue() 
          返回其值為此 BigDecimal 的非標度值 的 BigInteger。 
static BigDecimal valueOf(double val) 
          使用 Double.toString(double) 方法提供的 double 規範的字串表示形式將 double 轉換為 BigDecimal。 
static BigDecimal valueOf(long val) 
          將 long 值轉換為具有零標度的 BigDecimal。 
static BigDecimal valueOf(long unscaledVal, int scale) 
          將 long 非標度值和 int 標度轉換為 BigDecimal。

4、例項

  轉換

  將int、long、double、string型別的數值轉為BigDecimal。使用double會造成精度丟失,而使用BigDecimal就是為了解決精度丟失的問題,建議使用String方式轉換。、
  

BigDecimal b2 = new BigDecimal(1.23456789);//會造成精度丟失
BigDecimal b1 = new BigDecimal("1.23456789");//不會造成精度丟失
b1.setScale(3, BigDecimal.ROUND_HALF_EVEN);//設定精度,在保留小數位時,要設定舍入模式

反之,將BigDecimal轉int、long、float、double型別。

BigDecimal b1 = new BigDecimal("1.23456789");

System.out.println(b1.doubleValue());
System.out.println(b1.floatValue());
System.out.println(b1.longValue());
System.out.println(b1.intValue());
System.out.println(b1.toString());
  執行結果:

1.23456789
1.2345679
1
1
1.23456789

加減乘除

  可以進行正負數運算和顯示。
  

BigDecimal big1 = new BigDecimal("600");
BigDecimal big2 = new BigDecimal("300");
// 兩個BigDecimal相加
big1 = big1.add(big2);
// 兩個BigDecimal相減,
big1 = big1.subtract(big2);
// 兩個BigDecimal相乘
big1 = big1.multiply(big2);
// 兩個BigDecimal相除。
big1 = big1.divide(big2);

使用divide()方法注意:

相除的時候,被除數為0,會丟擲異常:java.lang.ArithmeticException: Division by zero。
相除後為小數,則應該保留小數位,否則可能會丟擲異常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result。

divide(BigDecimal divisor, int scale, int roundingMode);
b1.divide(b2, 2, BigDecimal.ROUND_CEILING);//採用ROUND_CEILING模式舍入,保留2位小數。

BigInteger類

  BigInteger類用法跟BigDecimal類用法很相似,就不多囉嗦。

  靜態欄位
  

static BigInteger ONE 
          BigInteger 的常量 1static BigInteger TEN 
          BigInteger 的常量 10static BigInteger ZERO 
          BigInteger 的常量 0

構造方法

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。 

方法
  

BigInteger abs() 
          返回其值是此 BigInteger 的絕對值的 BigInteger。 
 BigInteger add(BigInteger val) 
          返回其值為 (this + val) 的 BigInteger。 
 BigInteger and(BigInteger val) 
          返回其值為 (this & val) 的 BigInteger。 
 BigInteger andNot(BigInteger val) 
          返回其值為 (this & ~val) 的 BigInteger。 
 int bitCount() 
          返回此 BigInteger 的二進位制補碼錶示形式中與符號不同的位的數量。 
 int bitLength() 
          返回此 BigInteger 的最小的二進位制補碼錶示形式的位數,不包括 符號位。 
 BigInteger clearBit(int n) 
          返回其值與清除了指定位的此 BigInteger 等效的 BigInteger。 
 int compareTo(BigInteger val) 
          將此 BigInteger 與指定的 BigInteger 進行比較。 
 BigInteger divide(BigInteger val) 
          返回其值為 (this / val) 的 BigInteger。 
 BigInteger[] divideAndRemainder(BigInteger val) 
          返回包含 (this / val) 後跟 (this % val) 的兩個 BigInteger 的陣列。 
 double doubleValue() 
          將此 BigInteger 轉換為 double。 
 boolean equals(Object x) 
          比較此 BigInteger 與指定的 Object 的相等性。 
 BigInteger flipBit(int n) 
          返回其值與對此 BigInteger 進行指定位翻轉後的值等效的 BigInteger。 
 float floatValue() 
          將此 BigInteger 轉換為 float。 
 BigInteger gcd(BigInteger val) 
          返回一個 BigInteger,其值是 abs(this) 和 abs(val) 的最大公約數。 
 int getLowestSetBit() 
          返回此 BigInteger 最右端(最低位)1 位元的索引(即從此位元組的右端開始到本位元組中最右端 1 位元之間的 0 位元的位數)。 
 int hashCode() 
          返回此 BigInteger 的雜湊碼。 
 int intValue() 
          將此 BigInteger 轉換為 int。 
 boolean isProbablePrime(int certainty) 
          如果此 BigInteger 可能為素數,則返回 true,如果它一定為合數,則返回 false。 
 long longValue() 
          將此 BigInteger 轉換為 long。 
 BigInteger max(BigInteger val) 
          返回此 BigInteger 和 val 的最大值。 
 BigInteger min(BigInteger val) 
          返回此 BigInteger 和 val 的最小值。 
 BigInteger mod(BigInteger m) 
          返回其值為 (this mod m) 的 BigInteger。 
 BigInteger modInverse(BigInteger m) 
          返回其值為 (this-1 mod m) 的 BigInteger。 
 BigInteger modPow(BigInteger exponent, BigInteger m) 
          返回其值為 (thisexponent mod m) 的 BigInteger。 
 BigInteger multiply(BigInteger val) 
          返回其值為 (this * val) 的 BigInteger。 
 BigInteger negate() 
          返回其值是 (-this) 的 BigInteger。 
 BigInteger nextProbablePrime() 
          返回大於此 BigInteger 的可能為素數的第一個整數。 
 BigInteger not() 
          返回其值為 (~this) 的 BigInteger。 
 BigInteger or(BigInteger val) 
          返回其值為 (this | val) 的 BigInteger。 
 BigInteger pow(int exponent) 
          返回其值為 (thisexponent) 的 BigInteger。 
static BigInteger probablePrime(int bitLength, Random rnd) 
          返回有可能是素數的、具有指定長度的正 BigInteger。 
 BigInteger remainder(BigInteger val) 
          返回其值為 (this % val) 的 BigInteger。 
 BigInteger setBit(int n) 
          返回其值與設定了指定位的此 BigInteger 等效的 BigInteger。 
 BigInteger shiftLeft(int n) 
          返回其值為 (this << n) 的 BigInteger。 
 BigInteger shiftRight(int n) 
          返回其值為 (this >> n) 的 BigInteger。 
 int signum() 
          返回此 BigInteger 的正負號函式。 
 BigInteger subtract(BigInteger val) 
          返回其值為 (this - val) 的 BigInteger。 
 boolean testBit(int n) 
          當且僅當設定了指定的位時,返回 true。 
 byte[] toByteArray() 
          返回一個 byte 陣列,該陣列包含此 BigInteger 的二進位制補碼錶示形式。 
 String toString() 
          返回此 BigInteger 的十進位制字串表示形式。 
 String toString(int radix) 
          返回此 BigInteger 的給定基數的字串表示形式。 
static BigInteger valueOf(long val) 
          返回其值等於指定 long 的值的 BigInteger。 
 BigInteger xor(BigInteger val) 
          返回其值為 (this ^ val) 的 BigInteger。 

原文地址