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 开发者_Python百科find a method to go the other way around in Java.
You have a parameterized constructor for that.
BigDecimal(BigInteger val)
There is a constructor for that.
BigDecimal bigdec = new BigDecimal(bigint);
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
.
Documentation
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;
}
精彩评论