Java BigDecimal unscaledValue() Method with Example

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

BigDecimal Class unscaledValue() method

  • unscaledValue() method is available in java.math package.
  • unscaledValue() method is used to calculate the unscaled value by using the formula ([this BigDecimal] * 10 pow [this BigDecimal.scale()] ) of this BigDecimal as a BigInteger.
  • unscaledValue() 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.
  • unscaledValue() method does not throw an exception at the time of representing unscaled value.

Syntax:

    public BigInteger unscaledValue();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is BigInteger, it returns BigInteger of unscaled value of this BigDecimal.

Example:

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

import java.math.*;

public class UnscaledValueOfBD {
    public static void main(String args[]) {
        // Initialize three variables val1,
        // val2 and str 
        String val1 = "100.12";
        double val2 = 0.0;
        String str = "-2.346";

        // Initialize three BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(val1);
        BigDecimal b_dec2 = new BigDecimal(val2);
        BigDecimal b_dec3 = new BigDecimal(str);

        // returns the BigInteger that holds 
        // the unscaled value of this BigDecimal
        // b_dec1 and it is calculated by using
        // b_dec1 * 10 pow (2)
        BigInteger unscaled_val = b_dec1.unscaledValue();
        System.out.println("b_dec1.unscaledValue(): " + unscaled_val);

        // returns the BigInteger that holds 
        // the unscaled value of this BigDecimal
        // b_dec2 and it is calculated by using
        // b_dec2 * 10 pow (0)
        unscaled_val = b_dec2.unscaledValue();
        System.out.println("b_dec2.unscaledValue(): " + unscaled_val);

        // returns the BigInteger that holds 
        // the unscaled value of this BigDecimal
        // b_dec3 and it is calculated by using
        // b_dec3 * 10 pow (3)
        unscaled_val = b_dec3.unscaledValue();
        System.out.println("b_dec3.unscaledValue(): " + unscaled_val);
    }
}

Output

b_dec1.unscaledValue(): 10012
b_dec2.unscaledValue(): 0
b_dec3.unscaledValue(): -2346


Comments and Discussions!

Load comments ↻





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