Java.math.BigDecimal.pow() 方法



描述

java.math.BigDecimal.pow(int n, MathContext mc) 方法返回的 BigDecimal 值是 (thisn)。當前實現使用 ANSI 標準 X3.274-1996 中定義的核心演算法,並根據上下文設定進行舍入。

一般而言,返回的數值在所選精度的兩個 ulps 內處於精確數值。

宣告

以下是 java.math.BigDecimal.pow() 方法的宣告。

public BigDecimal pow(int n, MathContext mc)

引數

  • n − 將此 BigDecimal 提升到的冪。

  • mc − 要使用的上下文。

返回值

此方法返回使用 ANSI 標準 X3.274-1996 演算法將 BigDecimal 物件提升到 n 次方(即 thisn)的值。

異常

ArithmeticException − 如果結果不準確,但舍入模式為不需要,或 n 超出範圍。

示例

以下示例顯示了 math.BigDecimal.pow() 方法的使用。

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 2 BigDecimal Objects
      BigDecimal bg1, bg2;

      MathContext mc = new MathContext(4); // 4 precision

      bg1 = new BigDecimal("2.17");

      // apply pow method on bg1 using mc
      bg2 = bg1.pow(3, mc);

      String str = "The value of " + bg1 + " to the power of 3, rounded to " + bg2;

      // print bg2 value
      System.out.println( str );
   }
}

讓我們編譯並執行上面的程式,它將產生以下結果 −

The value of 2.17 to the power of 3, rounded to 10.22
java_math_bigdecimal.htm
廣告