Java BigInteger Class | doubleValue() Method with Example

BigInteger Class doubleValue() method: Here, we are going to learn about the doubleValue() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 10, 2020

BigInteger Class doubleValue() method

  • doubleValue() method is available in java.math package.
  • doubleValue() method is used to convert this BigInteger into a double and when this BigInteger value is large enough to fit in a long so it will be converted to either Double.POSITIVE_INFINITY or NEGATIVE_INFINITY.
  • doubleValue() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • doubleValue() method does not throw an exception at the time of conversion to a double.

Syntax:

    public double doubleValue();

Parameter(s):

  • None

Return value:

The return type of this method is double, it returns the value of this BigInteger into a double.

Example:

// Java program to demonstrate the example 
// of double doubleValue() method of BigInteger

import java.math.*;

public class DoubleValueOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2
        String str1 = "8023";
        String str2 = "100";

        // Initialize two BigInteger objects  
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);

        // converts this BigInteger (b_int1) into
        // a double, and store the result in a variable 
        // named d_conv

        double d_conv = b_int1.doubleValue();
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int1.doubleValue(): " + d_conv);

        System.out.println();

        // converts this BigInteger (b_int2) into
        // a double, and store the result it in a variable 
        // named d_conv
        d_conv = b_int2.doubleValue();
        System.out.println("b_int2: " + b_int2);
        System.out.println("b_int2.doubleValue(): " + d_conv);
    }
}

Output

b_int1: 8023
b_int1.doubleValue(): 8023.0

b_int2: 100
b_int2.doubleValue(): 100.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.