Java.math.BigInteger.doubleValue() Method



Description

The java.math.BigInteger.doubleValue() converts this BigInteger to a double. This conversion is similar to the narrowing primitive conversion from double to float.

If this BigInteger has too great a magnitude to represent as a double, it will be converted to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate. Even when the return value is finite, this conversion can lose information about the precision of the BigInteger value.

Declaration

Following is the declaration for java.math.BigInteger.doubleValue() method.

public double doubleValue()

Specified by

doubleValue in class Number.

Parameters

NA

Return Value

This method returns this BigInteger converted to a double.

Exception

NA

Example

The following example shows the usage of math.BigInteger.doubleValue() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 2 BigInteger objects
      BigInteger bi1, bi2;

      // create 2 Double objects
      Double d1, d2;

      // assign value to bi1
      bi1 = new BigInteger("123");

      // assign a larger value to bi2
      bi2 = new BigInteger("12345678");

      // assign double value of bi1, bi2 to d1, d2
      d1 = bi1.doubleValue();
      d2 = bi2.doubleValue();

      String str1 = "Double value of " + bi1 + " is " +d1;
      String str2 = "Double value of " + bi2 + " is " +d2;

      // print d1, d2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Let us compile and run the above program, this will produce the following result −

Double value of 123 is 123.0
Double value of 12345678 is 1.2345678E7
java_math_biginteger.htm
Advertisements