Java BigDecimal intValue() Method with Example

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

BigDecimal Class intValue() method

  • intValue() method is available in java.math package.
  • intValue() method is used to convert a BigDecimal to an integer and when the converted BigDecimal value is large enough to fit into an integer then in that case low order 32 bits are to be retrieved and the returned value is with opposite sign.
  • intValue() 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.
  • intValue() method does not throw an exception at the time of converting BigDecimal to int.

Syntax:

    public int intValue();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it gets the integer representation of this BigDecimal.

Example:

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

import java.math.*;

public class IntValueOfBD {
    public static void main(String args[]) {
        // Initialize two variables first is
        // of "double" and second is of "String"
        // type
        double val = 115.23;
        String str = "100";

        // Initialize two BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(val);
        BigDecimal b_dec2 = new BigDecimal(str);

        // convert this BigDecimal (b_dec1) into
        // an int, variable named i_conv
        int i_conv = b_dec1.intValue();
        System.out.println("b_dec1.intValue(): " + i_conv);

        // convert this BigDecimal (b_dec2) into
        // an int, variable named i_conv
        i_conv = b_dec2.intValue();
        System.out.println("b_dec2.intValue(): " + i_conv);
    }
}

Output

b_dec1.intValue(): 115
b_dec2.intValue(): 100


Comments and Discussions!

Load comments ↻





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