Regis
Regis

Reputation: 421

How to convert BigInteger to BigDecimal?

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.

Upvotes: 42

Views: 62559

Answers (4)

Pragnesh Modh
Pragnesh Modh

Reputation: 81

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

Upvotes: 6

EAOE
EAOE

Reputation: 63

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

Upvotes: -2

bdhar
bdhar

Reputation: 23003

You have a parameterized constructor for that.

BigDecimal(BigInteger val)

Upvotes: 69

erickson
erickson

Reputation: 269857

There is a constructor for that.

BigDecimal bigdec = new BigDecimal(bigint);

Upvotes: 30

Related Questions