Java BigDecimal toBigInteger() Method with Example

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

BigDecimal Class toBigInteger() method

  • toBigInteger() method is available in java.math package.
  • toBigInteger() method is used to convert this BigDecimal object value into a BigInteger object value and the decimal part will be skipped during the conversion of this BigDecimal.
  • toBigInteger() 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.
  • toBigInteger() method does not throw an exception at the time of converting this BigDecimal into BigInteger.

Syntax:

    public BigInteger toBigInteger();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is BigInteger, it returns the conversion of this BigDecimal into a BigInteger.

Example:

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

import java.math.*;

public class ToBigIntegerOfBD {
    public static void main(String args[]) {
        // Initialize two variables - double and 
        // String type
        double val = 1234521.000;
        String str = "1211124";

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

        // converts this BigDecimal b_dec1 value into
        // a BigInteger value, and store the result
        // in a variable named b_int_conv
        BigInteger b_int_conv = b_dec1.toBigInteger();
        System.out.println("b_dec1.toBigInteger(): " + b_int_conv);

        // converts this BigDecimal b_dec2 value into
        // a BigInteger value, and store the result
        // in a variable named b_int
        b_int_conv = b_dec2.toBigInteger();
        System.out.println("b_dec2.toBigInteger(): " + b_int_conv);
    }
}

Output

b_dec1.toBigInteger(): 1234521
b_dec2.toBigInteger(): 1211124


Comments and Discussions!

Load comments ↻





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