如何将BigInteger转换为BigDecimal?

时间:2021-01-26 16:53:09

Is there any way to convert a BigInteger into a BigDecimal?

有没有办法将BigInteger转换为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.

我知道你可以从BigDecimal转到BigInteger,但我找不到一种方法来反过来使用Java。

4 个解决方案

#1


35  

You have a parameterized constructor for that.

你有一个参数化的构造函数。

BigDecimal(BigInteger val)

BigDecimal(BigInteger val)

#2


17  

There is a constructor for that.

有一个构造函数。

BigDecimal bigdec = new BigDecimal(bigint);

#3


5  

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/10scale).

public BigDecimal(BigInteger unscaledVal,int scale)将BigInteger非标度值和int标度转换为BigDecimal。 BigDecimal的值是(unscaledVal / 10scale)。

Parameters:

参数:

unscaledVal - unscaled value of the BigDecimal. scale - scale of the BigDecimal.

unscaledVal - BigDecimal的未缩放值。 scale - BigDecimal的比例。

Throws:

抛出:

NumberFormatException - scale is negative

NumberFormatException - 比例为负数

Good luck!!!

祝你好运!!!

#4


1  

     BigInteger I = new BigInteger("123")
     BigDecimal bd = BigDecimal.valueOf(I.intValue());
     System.out.println("The Value of bd : "+ bd);

This will convert the BigInteger value of I into BigDecimal value.

这会将I的BigInteger值转换为BigDecimal值。

#1


35  

You have a parameterized constructor for that.

你有一个参数化的构造函数。

BigDecimal(BigInteger val)

BigDecimal(BigInteger val)

#2


17  

There is a constructor for that.

有一个构造函数。

BigDecimal bigdec = new BigDecimal(bigint);

#3


5  

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/10scale).

public BigDecimal(BigInteger unscaledVal,int scale)将BigInteger非标度值和int标度转换为BigDecimal。 BigDecimal的值是(unscaledVal / 10scale)。

Parameters:

参数:

unscaledVal - unscaled value of the BigDecimal. scale - scale of the BigDecimal.

unscaledVal - BigDecimal的未缩放值。 scale - BigDecimal的比例。

Throws:

抛出:

NumberFormatException - scale is negative

NumberFormatException - 比例为负数

Good luck!!!

祝你好运!!!

#4


1  

     BigInteger I = new BigInteger("123")
     BigDecimal bd = BigDecimal.valueOf(I.intValue());
     System.out.println("The Value of bd : "+ bd);

This will convert the BigInteger value of I into BigDecimal value.

这会将I的BigInteger值转换为BigDecimal值。