当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。