Java BigDecimal pow() Method with Example

BigDecimal Class pow() method: Here, we are going to learn about the pow() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 05, 2020

BigDecimal Class pow() method

Syntax:

    public BigDecimal pow(int x);
    public BigDecimal pow(int x, MathContext ma_co);
  • pow() method is available in java.math package.
  • pow(int x) method is used to calculate the value of this BigDecimal is raised to the power of the given parameter (x) (i.e. [(this BigDecimal) pow (x)]).
  • pow(int x, MathContext ma_co) method is used to calculate the value of this BigDecimal is raised to the power of the given parameter (x) (i.e. [(this BigDecimal) pow (x)]) along with rounding based on the given MathContext settings.
  • These methods may throw an exception at the time of calculating power.
    ArithmeticException: This exception may throw when the result is not accurate and set the rounding mode "UNNECESSARY".
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, pow(int x),
    • int x – represents the power value to raise to this BigDecimal object.
  • In the first case, pow(int x, MathContext ma_co),
    • int x – Similar as defined in the first case.
    • MathContext ma_co – represents the context setting to use in rounding.

Return value:

In both the cases, the return type of the method is BigDecimal,

  • In the first case, it returns the value of this BigDecimal is raised to the power of the given argument.
  • In the second case, it returns the value of this BigDecimal is raised to the power of the given argument it is implemented by the algo ANSI Standards X3.274 - 1996.

Example:

// Java program to demonstrate the example 
// of pow() method of BigDecimal

import java.math.*;

public class PowOfBD {
    public static void main(String args[]) {
        // Initialize two variables val,
        // exp 
        String val = "10.30";
        int exp = 2;

        // Initialize a BigDecimal objects and
        // one MathContext
        BigDecimal b_dec = new BigDecimal(val);
        MathContext ma_co = new MathContext(3, RoundingMode.CEILING);

        System.out.println("pow(int): ");

        // calculates the pow of this BigDecimal b_dec is
        // raised to the power of the given argument
        // exp like b_dec pow(exp)
        BigDecimal pow = b_dec.pow(exp);
        System.out.println("b_dec.pow(exp): " + pow);

        System.out.println(" ");

        System.out.println("pow(int,MathContext): ");

        // calculates the pow of this BigDecimal b_dec is
        // raised to the power of the given argument
        // exp like b_dec pow(exp) and return the result
        // is based on the given context ma_co
        pow = b_dec.pow(exp, ma_co);
        System.out.println("b_dec.pow(exp,ma_co): " + pow);
    }
}

Output

pow(int): 
b_dec.pow(exp): 106.0900
 
pow(int,MathContext): 
b_dec.pow(exp,ma_co): 107


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.