How to convert BigInteger to BigDecimal or BigDecimal to BigInteger in Java with example

In this blog post, learn How to Convert BigInteger from/to BigDecimal with examples.

BigInteger and BigDecimal examples

Integer, Long, and Double are basic numeric primitive types that store numeric values up to a limited range of values for basic arithmetic operations. BigInteger is used to store large numeric values. BigDecimal is used to store the correct precision and rounding numbers where precision is important in financial applications.

BigInteger and BigDecimal are classes in the java. math package

You can also check my previous posts on BigInteger class in java.

How to convert BigInteger to BigDecimal in java?

There are two ways we can convert from BigInteger to BigDecimal.

Using BigDecimal constructor

BigDecimal has the below parameter constructor with accepts the BigInteger value and returns thebigDecimal value.

Syntax:

   BigDecimal(BigInteger unscaledvalue)
   BigDecimal(BigInteger unscaled value, int scaledValue)

First Constructor, accepts BigInteger argument with zero scaled value. The second Constructor accepts BigInteger and scaled values.

It converts to BigDecimal with a scaled value applied. NumberFormatException throws if the given scale is negative.

import java.math.BigDecimal;
import java.math.BigInteger;
public class Test11 {
    public static void main(String[] args) {

        BigInteger biginteger = new BigInteger("4785961234");
        // Constructor without scale value
        BigDecimal bd = new BigDecimal(biginteger);
        System.out.println(bd);

        BigInteger biginteger1 = new BigInteger("78459789");
        // Constructor with scale value
        BigDecimal bd1 = new BigDecimal(biginteger1,2);
        System.out.println(bd1);
    }
}

Outputs:

4785961234
784597.89

Using BigDecimal.valueOf method

BigDecimal valueOf converts and returns to BigDecimal.

The only limitation with this is if bigintger contains a larger number as shown below. It will give the wrong number as intValue used.

you can use intValue or longValue method of BigInteger in the valueOf method.

import java.math.BigDecimal;
import java.math.BigInteger;
public class Test11 {
    public static void main(String[] args) {

        BigInteger bi = new BigInteger("789456");
        BigDecimal bd = BigDecimal.valueOf(bi.longValue());
        System.out.println(bd);
        BigInteger bi1 = new BigInteger("78945612312312312312312");
        BigDecimal bd1 = BigDecimal.valueOf(bi1.longValue());
        System.out.println(bd1);
    }
}

Outputs

789456
-6452323164568604168

How to Convert BigDecimal to BigInteger in java?

BigDecimal contains precision, whereas BigInteger does not contain decimal parts.

when we converted BigDecimal to BigInteger, bigdecimal precision is discarded. BigDecimal provides toBigInteger() method -

returns the double/long value in the Biginteger object and returns it.

It loses decimal parts after converting to BigInteger.

BigDecimal has a method toBigInteger that converts to BigInteger.

Syntax:

    public BigInteger toBigInteger()

Here is an example for Converting BigInteger to BigDecimal in java

import java.math.BigDecimal;
import java.math.BigInteger;
public class Test11 {
    public static void main(String[] args) {

        BigDecimal bigDecimal = new BigDecimal("457695216.89566");
        BigInteger bigInteger=bigDecimal.toBigInteger();
        System.out.println(bigInteger.getClass());
        System.out.println(bigInteger);
    }
}

Output:

class java.math.BigInteger
457695216

Summary

Learned multiple ways to convert Biginteger to/ from BIgDecimal in java.