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


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


java.math.BigDecimal.pow(int n)方法用於計算BigDecimal提高到作為參數傳遞的其他一些正數的冪(this)n。此方法執行將BigDecimal數提高為作為參數傳遞的正數的冪。

用法:

public BigDecimal pow(int n)

參數:此方法接受參數n,這是我們希望將BigDecimal的冪次冪的指數。


返回:此方法返回一個BigDecimal,它等於(this)n

異常:參數n必須在0到999999999之間(包括0和999999999),否則會引發ArithmeticException

以下示例程序旨在說明BigDecimal類的pow()方法

範例1:

// Java program to demonstrate 
// pow() method of BigDecimal 
  
import java.math.BigDecimal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // Creating BigDecimal object 
        BigDecimal b1 
            = new BigDecimal("924567"); 
  
        // Exponent or power 
        int n = 5; 
  
        // Using pow() method 
        BigDecimal result = b1.pow(n); 
  
        // Display result 
        System.out.println("Result of pow operation "
                           + "between BigDecimal "
                           + b1 + " and exponent "
                           + n + " equal to "
                           + result); 
    } 
}
輸出:

Result of pow operation between BigDecimal 924567 and exponent 5 equal to 675603579456764176151564447607

範例2:

// Java program to demonstrate 
// pow() method of BigDecimal 
  
import java.math.BigDecimal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // Creating BigDecimal object 
        BigDecimal b1 
            = new BigDecimal("457863"); 
  
        // Exponent or power 
        int n = 4; 
  
        // Using pow() method 
        BigDecimal result = b1.pow(n); 
  
        // Display result 
        System.out.println("Result of pow operation "
                           + "between BigDecimal "
                           + b1 + " and exponent "
                           + n + " equal to "
                           + result); 
    } 
}
輸出:

Result of pow operation between BigDecimal 457863 and exponent 4 equal to 43948311905876729579361

範例3:程序在作為參數傳遞的指數小於零時顯示異常。

// Java program to demonstrate 
// pow() method of BigDecimal 
  
import java.math.BigDecimal; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        // Creating BigDecimal object 
        BigDecimal b1 
            = new BigDecimal("10000"); 
  
        // Negative power 
        int n = -1; 
  
        try { 
            // Using pow() method 
            BigDecimal result = b1.pow(n); 
  
            // Display result 
            System.out.println("Result of pow operation "
                               + "between BigDecimal "
                               + b1 + " and exponent "
                               + n + " equal to "
                               + result); 
        } 
        catch (Exception e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:

java.lang.ArithmeticException: Invalid operation

參考: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigDecimal.html#pow(int)



相關用法


注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 BigDecimal pow() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。