Java BigDecimal scaleByPowerOfTen() Method with Example

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

BigDecimal Class scaleByPowerOfTen() method

  • scaleByPowerOfTen() method is available in java.math package.
  • scaleByPowerOfTen() method is used to get a BigDecimal whose value is calculated by using [(this BigDecimal) * 10 pow(number)].
  • scaleByPowerOfTen() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • scaleByPowerOfTen() method may throw an exception at the time of scaling by the power of 10.
    ArithmeticException: This exception may throw when the scale value is not in a range.

Syntax:

    public BigDecimal scaleByPowerOfTen(int number);

Parameter(s):

  • int number – represents the number raised to the power of 10 * (this BigDecimal) to calculated BigDecimal.

Return value:

The return type of this method is BigDecimal, it returns the BigDecimal object whose value is calculated by using (10 pow[number] * [this BigDecimal]).

Example:

// Java program to demonstrate the example 
// of BigDecimal scaleByPowerOfTen(int number) method of BigDecimal

import java.math.*;

public class ScaleByPowerOfTenOfBD {
    public static void main(String args[]) {
        // Initialize two variables of int and 
        // String type
        int val = 3524;
        String str = "100.123";
        
        // Initialize two BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(val);
        BigDecimal b_dec2 = new BigDecimal(str);

        // returns the BigDecimal that holds the value
        // of this BigDecimal b_dec1 is multiplied by
        // 10 raised to the power of the given int "2"
        BigDecimal bd = b_dec1.scaleByPowerOfTen(2);
        System.out.println("b_dec1.scaleByPowerOfTen(2): " + bd);

        // returns the BigDecimal that holds the value
        // of this BigDecimal b_dec2 is multiplied by
        // 10 raised to the power of given int "3"
        bd = b_dec2.scaleByPowerOfTen(3);
        System.out.println("b_dec2.scaleByPowerOfTen(3): " + bd);
    }
}

Output

b_dec1.scaleByPowerOfTen(2): 3.524E+5
b_dec2.scaleByPowerOfTen(3): 100123


Comments and Discussions!

Load comments ↻





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