Java Convert BigDecimal to BigInteger

Tags: BigDecimal BigInteger

In this Java core tutorial we learn how to convert a java.math.BigDecimal object to a java.math.BigInteger object in Java programming language.

How to convert BigDecimal to BigInteger in Java

In Java with a given BigDecimal value we can use the BigDecimal.toBigInteger() method to convert it to a BigInteger value as the following Java program.

ConvertBigDecimalToBigIntegerExample1.java

import java.math.BigDecimal;
import java.math.BigInteger;

public class ConvertBigDecimalToBigIntegerExample1 {
    public static void main(String... args) {
        BigDecimal bigDecimal = new BigDecimal("123456.999");

        BigInteger bigInteger = bigDecimal.toBigInteger();

        System.out.println("BigDecimal: " + bigDecimal);
        System.out.println("BigInteger: " + bigInteger);
    }
}
The output as below.
BigDecimal: 123456.999
BigInteger: 123456

We can also use the BigDecimal.toBigIntegerExact() method to convert a BigDecimal object to BigInteger object with an exception thrown if the BigDecimal value has nonzero factional part.

ConvertBigDecimalToBigIntegerExample2.java

import java.math.BigDecimal;
import java.math.BigInteger;

public class ConvertBigDecimalToBigIntegerExample2 {
    public static void main(String... args) {
        BigDecimal bigDecimal = new BigDecimal("99999999");

        BigInteger bigInteger = bigDecimal.toBigIntegerExact();

        System.out.println("BigDecimal: " + bigDecimal);
        System.out.println("BigInteger: " + bigInteger);
    }
}
The output as below.
BigDecimal: 99999999
BigInteger: 99999999

The example Java code below to show the BigDecimal value which throw a java.lang.ArithmeticException exception when we use the toBigIntegerExact() method to convert it.

ConvertBigDecimalToBigIntegerExample3.java

import java.math.BigDecimal;
import java.math.BigInteger;

public class ConvertBigDecimalToBigIntegerExample3 {
    public static void main(String... args) {
        BigDecimal bigDecimal = new BigDecimal("1.2");

        BigInteger bigInteger = bigDecimal.toBigIntegerExact();
    }
}
The output as below.
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
	at java.base/java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4628)
	at java.base/java.math.BigDecimal.needIncrement(BigDecimal.java:4684)
	at java.base/java.math.BigDecimal.divideAndRound(BigDecimal.java:4592)
	at java.base/java.math.BigDecimal.setScale(BigDecimal.java:2892)
	at java.base/java.math.BigDecimal.toBigIntegerExact(BigDecimal.java:3488)
	at ConvertBigDecimalToBigIntegerExample3.main(ConvertBigDecimalToBigIntegerExample3.java:8)

Happy Coding 😊

How to Negate BigDecimal Value in Java

How to Round BigDecimal Value in Java

How to Get Absolute of BigDecimal Value in Java

Java Convert BigInteger to BigDecimal

Java Convert long to BigDecimal

Java Convert int to BigDecimal

Java Convert double to BigDecimal

Java Convert float to BigDecimal

How to use BigDecimal in Java by Examples

How to Add two BigDecimal values in Java

How to Subtract two BigDecimal values in Java

How to Multiply two BigDecimal values in Java

How to Divide two BigDecimal values in Java

Java Convert BigDecimal value into double value

Java Convert BigDecimal value into float value

Java Convert BigDecimal value into int value

Java Convert BigDecimal value into long value

Java Convert BigDecimal value into short value

Java Convert BigDecimal value to byte value

Java Convert BigDecimal to String

Java Convert String to BigDecimal

Java Compare two BigDecimal values