Reputation: 421
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
Reputation: 81
public BigDecimal(BigInteger unscaledVal, int scale)
Translates a
BigInteger
unscaled value and anint
scale into aBigDecimal
. The value of theBigDecimal
isunscaledVal/10^scale
.Parameters:
unscaledVal
- unscaled value of theBigDecimal
.
scale
- scale of theBigDecimal
.
Upvotes: 6
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
Reputation: 23003
You have a parameterized constructor for that.
BigDecimal(BigInteger val)
Upvotes: 69
Reputation: 269857
There is a constructor for that.
BigDecimal bigdec = new BigDecimal(bigint);
Upvotes: 30