Signup/Sign In

Java Double longBitsToDouble() method

Java longBitsToDouble() method is a part of the Double class of the java.lang package. This method returns the double value of the long bit passed as an argument in accordance with the IEEE 754 floating-point 'double format' bit layout.

Syntax:

public static double longBitsToDouble(long bits)  

Parameters:

The parameter passed is the long value whose double value is to be returned.

Returns:

The equivalent double value of the long bits passed.

Example 1:

Here, the long bit values are converted to its equivalent double value.

import java.lang.Double;
public class StudyTonight
{     
 public static void main(String[] args) 
   {  
  
             long n1 = 69;
             long n2 = -645;
  
             Double d1 = Double.longBitsToDouble(n1);  
             System.out.println("value after conversion = "+d1);
             
             Double d2 = Double.longBitsToDouble(n1);  
             System.out.println("value after conversion = "+d2);
    }  
}  


value after conversion = 3.4E-322
value after conversion = 3.4E-322

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Double;
import java.util.Scanner;
public class StudyTonight
{  
    public static void main(String[] args)
    {  
        try
        {
            System.out.println("Enter value");
            Scanner sc = new Scanner(System.in);
            long n = sc.nextLong();
            System.out.println(" Double value is = "+ Double.longBitsToDouble(n)); //long bits converted to double 
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }        
    }    
}  


Enter value
556
Double value is = 2.747E-321
************************************
Enter value
-88
Double value is = NaN
************************************
Enter value
0x338
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.