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


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


描述

這個java.math.BigDecimal.pow(int n)返回一個 BigDecimal,其值為 (thisn), 冪計算精確,精度無限。

參數 n 必須在 0 到 999999999 的範圍內,包括 0 到 999999999。 ZERO.pow(0) 返回 1。

聲明

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

public BigDecimal pow(int n)

參數

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

返回值

此方法返回 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;

      bg1 = new BigDecimal("2.17");

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

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

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

讓我們編譯並運行上麵的程序,這將產生以下結果 -

The value of 2.17 to the power of 3 is 10.218313

相關用法


注:本文由純淨天空篩選整理自 Java.math.BigDecimal.pow() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。