How to convert BigInteger to/from BigDecimal in Java(with example)

This tutorial shows multiple ways to convert BigInteger to BigDecimal and BigInteger to BigDecimal in Java with examples using the BigDecimal constructor, using the toBigInteger method

BigInteger and BigDecimal examples

Integer, Long, and Double are basic primitive numeric types, designed to store numeric values with specific ranges.

BigInteger and BigDecimal are used to store large numbers. BigDecimal is utilized to store large numbers with precise accuracy, particularly in financial applications where correct precision and rounding are essential. So, when attempting to convert from BigDecimal to BigInteger, there is a loss of precision.

Both are immutable classes declared 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 below-overloaded constructors for big numbers. It accepts the BigInteger value and returns the corresponding bigDecimal value.

Syntax:

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

First Constructor, accepts BigInteger argument with default scaled value. The second Constructor accepts BigInteger and scaled values. The third constructor accepts a String and returns a value.

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);

          // Constructor with a string value throws an error if the string is not a number
        BigDecimal bd2 = new BigDecimal(biginteger1.toString());
        System.out.println(bd2);

    }
}

Outputs:

4785961234
784597.89
4785961234

Points:

  • It is simple and easy to read, It allows you to pass the value during object creation
  • If the input is a larger value, It results in a value with loss of precision.

Using BigDecimal.valueOf method

BigDecimal.valueOf() method converts, and returns to BigDecimal.

The only limitation 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

Points:

  • This approach has the flexibility to convert later once the object is created. It is a static factory method called on class names.
  • It results in a value with a loss of precision if the value is a larger bigger number.

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.

Multiple ways we can do this.

Use toBigInteger method

BigDecimal provides the toBigInteger() method, which returns a bigger value by removing precision.

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

It loses decimal parts after converting to BigInteger.

Syntax:

    public BigInteger toBigInteger()

Here is an example of 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

Use valueOf methods

BigDecimal provides the following methods

  • intValue(): Returns int value
  • longValue(): Returns long value

Pass the above values of the above methods into the valueOf() method

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

        BigDecimal bigDecimal = new BigDecimal("11.678");
        // Convert int value of an object
        BigInteger intValue=BigInteger.valueOf(bigDecimal.intValue());
        System.out.println(intValue);
        // Convert a long value of an object
        BigInteger longValue=BigInteger.valueOf(bigDecimal.longValue());
        System.out.println(longValue);
    }

}

Output:

11
11

Conclusion

To summarize, It is easy to convert a value to BigInteger and BigDecimal using multiple approaches.