36
votes

Is there any way to convert a BigInteger into a BigDecimal?

I know you can go from a BigDecimal to a BigInteger, but I can't find a method to go the other way around in Java.

4
you should accept answer at least - Andremoniy

4 Answers

59
votes

You have a parameterized constructor for that.

BigDecimal(BigInteger val)

24
votes

There is a constructor for that.

BigDecimal bigdec = new BigDecimal(bigint);
6
votes

public BigDecimal(BigInteger unscaledVal, int scale)

Translates a BigInteger unscaled value and an int scale into a BigDecimal. The value of the BigDecimal is unscaledVal/10^scale.

Parameters:

unscaledVal - unscaled value of the BigDecimal.
scale - scale of the BigDecimal.

Documentation

-1
votes

I know this reply is late but it will help new users looking for this solution, you can convert BigInteger to BigDecimal by first converting the BigInteger into a string then putting the string in the constructor of the BigDecimal, example :

      public static BigDecimal Format(BigInteger value) {
          String str = value.toString();
          BigDecimal _value = new BigDecimal(str);
          return _value;
      }