Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there anyway to convert from Double to BigInteger?

Is there anyway to convert from double value to BigInteger?

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

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

like image 847
Krack Avatar asked Jul 31 '13 01:07

Krack


People also ask

Is BigInteger bigger than long?

3. BigInteger Larger Than Long. MAX_VALUE. As we already know, the long data type is a 64-bit two's complement integer.


2 Answers

If you want to store the integral part of the double into a BigInteger, then you can convert it into a BigDecimal and then into a BigInteger:

BigInteger k = BigDecimal.valueOf(doubleValue).toBigInteger();
like image 67
bb94 Avatar answered Sep 20 '22 05:09

bb94


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

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.

like image 44
Kon Avatar answered Sep 20 '22 05:09

Kon