當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.math.BigDecimal.pow()用法及代碼示例


描述

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

通常,返回的數值在所選精度的精確數值的兩個 ul 以內。

聲明

以下是聲明java.math.BigDecimal.pow()方法。

public BigDecimal pow(int n, MathContext mc)

參數

  • n- 將這個 BigDecimal 提升到的權力。

  • mc- 要使用的上下文。

返回值

此方法返回 BigDecimal 對象的 n 次冪的值,即 thisn, 使用 ANSI 標準 X3.274-1996 算法。

異常

ArithmeticException− 如果結果不準確但舍入模式為 UNNECESSARY,或 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.pow() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。