是否有从双整数到大整数的转换?

时间:2022-04-06 16:52:46

Is there anyway to convert from double value to BigInteger?

是否存在将双值转换为BigInteger的方法?

double doubleValue = 64654679846513164.2;
BigInteger bigInteger = (BigInteger) doubleValue;

I try to cast it but it didn't work.

我试着浇铸它,但没有用。

3 个解决方案

#1


17  

BigInteger is made for holding arbitrary precision integer numbers, not decimals. You can use the BigDecimal class to hold a double.

BigInteger用于保存任意精度的整数,而不是小数。可以使用BigDecimal类来保存双精度浮点数。

BigDecimal k = BigDecimal.valueOf(doublevalue);

In general, you cannot type cast a Java primitive into another class. The exceptions that I know about are the classes extending Number, such as the Long and Integer wrapper classes, which allow you to cast an int value into an Integer, and so on.

通常,不能将Java原语类型转换为另一个类。我所知道的例外是扩展数字的类,如Long和Integer包装器类,它允许您将int值转换为整数,等等。

#2


31  

You can convert the double into a BigDecimal and then into a BigInteger:

你可以把double转换成BigDecimal,然后再转换成BigInteger:

BigInteger k = BigDecimal.valueOf(doublevalue).toBigInteger();

#3


-1  

yo cannot cast Double to BigInteger, beacause these two classes are peer classes extending from Number class. you may be cast to Number and then can do further conversion.

您不能将Double强制转换为BigInteger,因为这两个类是从Number类扩展而来的对等类。你可以被转换成数字,然后可以做进一步的转换。

#1


17  

BigInteger is made for holding arbitrary precision integer numbers, not decimals. You can use the BigDecimal class to hold a double.

BigInteger用于保存任意精度的整数,而不是小数。可以使用BigDecimal类来保存双精度浮点数。

BigDecimal k = BigDecimal.valueOf(doublevalue);

In general, you cannot type cast a Java primitive into another class. The exceptions that I know about are the classes extending Number, such as the Long and Integer wrapper classes, which allow you to cast an int value into an Integer, and so on.

通常,不能将Java原语类型转换为另一个类。我所知道的例外是扩展数字的类,如Long和Integer包装器类,它允许您将int值转换为整数,等等。

#2


31  

You can convert the double into a BigDecimal and then into a BigInteger:

你可以把double转换成BigDecimal,然后再转换成BigInteger:

BigInteger k = BigDecimal.valueOf(doublevalue).toBigInteger();

#3


-1  

yo cannot cast Double to BigInteger, beacause these two classes are peer classes extending from Number class. you may be cast to Number and then can do further conversion.

您不能将Double强制转换为BigInteger,因为这两个类是从Number类扩展而来的对等类。你可以被转换成数字,然后可以做进一步的转换。