Converting a BigInteger to a primitive type

Use the methods doubleValue, floatValue, intValue or longValue.

Main.java:

import java.math.*;
  
public class Main {
   public static void main(String args[]) {
      BigInteger bi1 = new BigInteger("123456789");
 
      int bdInt       = bi1.intValue();
      long bdLong     = bi1.longValue();
      float bdFloat   = bi1.floatValue();
      double bdDouble = bi1.doubleValue();
 
      System.out.println("Conversion " + bi1 + " to:");
      System.out.println("tint:t" + bdInt);
      System.out.println("tlong:t" + bdLong);
      System.out.println("tfloat:t" + bdFloat);
      System.out.println("tdouble:t" + bdDouble);
 
      BigInteger bi2 = new BigInteger("123456789012345678901234567890");
 
      System.out.println("Conversion " + bi2 + " to:");
      System.out.println("tint:t" + bi2.intValue());
      System.out.println("tlong:t" + bi2.longValue());
      System.out.println("tfloat:t" + bi2.floatValue());
      System.out.println("tdouble:t" + bi2.doubleValue());
   } 
}

outputs:

Conversion 123456789 to:
	int:	123456789
	long:	123456789
	float:	1.23456792E8
	double:	1.23456789E8
Conversion 123456789012345678901234567890 to:
	int:	1312754386
	long:	-4362896299872285998
	float:	1.2345679E29
	double:	1.2345678901234568E29

As a BigInteger has an arbitrary precision, it is very important to remember how the conversion
occurs and what happens when the number is too large to fit in the primitive type.

int:    only the low-order 32 bits will be taken for conversion
 
long:	only the low-order 64 bits will be taken for conversion
 
float:  will have positive or negative infinity, can be tested with the 
        finals Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY
 
double: will have positive or negative infinity, can be tested with the 
        finals Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY