42

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.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Regis
  • 421
  • 1
  • 4
  • 3

4 Answers4

67

You have a parameterized constructor for that.

BigDecimal(BigInteger val)

rbrito
  • 2,398
  • 2
  • 21
  • 24
bdhar
  • 21,619
  • 17
  • 70
  • 86
29

There is a constructor for that.

BigDecimal bigdec = new BigDecimal(bigint);
erickson
  • 265,237
  • 58
  • 395
  • 493
6

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

ZygD
  • 22,092
  • 39
  • 79
  • 102
-1

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

EAOE
  • 65
  • 6
  • 1
    Why would you want to do that? Given that there exists already a constructor made specifically for this purpose (see other answers). I do not see any benefits that this approach offers. – Zabuzard Feb 26 '21 at 17:38