Saturday, September 6, 2025
HomeLanguagesJavaBigDecimal toBigIntegerExact() Method in Java

BigDecimal toBigIntegerExact() Method in Java

The java.math.BigDecimal.toBigIntegerExact() is an inbuilt method in java that converts this BigDecimal to a BigInteger, checking for lost information. An exception is thrown if this BigDecimal has a nonzero fractional part.

Syntax:

public BigInteger toBigIntegerExact()

Parameters: The method does not take any parameters.

Return value: This method returns the value of BigDecimal object converted to a BigInteger.

Examples:

Input: (BigDecimal) 1213
Output: (BigInteger) 1213

Input: (BigDecimal) 12785412
Output: (BigInteger) 12785412

Below programs illustrates the working of the above mentioned method:
Program 1:




// Program to demonstrate toBigIntegerExact() method of BigDecimal 
  
import java.math.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Assigning BigDecimal b
        BigDecimal b = new BigDecimal("1213");
  
        // Assigning the BigIntegerExact value of BigInteger b to  BigInteger  i
        BigInteger i = b.toBigIntegerExact();
  
        // Printing i value
        System.out.println("BigInteger value of " + b + " is " + i);
    }
}


Output:

BigInteger value of 1213 is 1213

Program 2:




// Program to demonstrate toBigIntegerExact() method of BigDecimal 
  
import java.math.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // Assigning BigDecimal b
        BigDecimal b = new BigDecimal("12785412");
  
        // Assigning the BigIntegerExact value of BigInteger b to  BigInteger  i
        BigInteger i = b.toBigIntegerExact();
  
        // Printing i value
        System.out.println("BigInteger value of " + b + " is " + i);
    }
}


Output:

BigInteger value of 12785412 is 12785412
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS