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); } } |
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); } } |
BigInteger value of 12785412 is 12785412
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- BigDecimal min() Method in Java
- BigDecimal max() Method in Java
- BigDecimal abs() Method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal byteValueExact() Method in Java
- BigDecimal doubleValue() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal toBigInteger() Method in Java
- BigDecimal stripTrailingZeros() Method in Java
- BigDecimal equals() Method in Java
- BigDecimal shortValueExact() Method in Java
- BigDecimal signum() Method in Java
- BigDecimal longValue() Method in Java
- BigDecimal hashCode() Method in Java
- BigDecimal precision() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal intvalueExact() Method in Java
- BigDecimal round() Method in Java
- BigDecimal scale() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

