开发者

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜