Signup/Sign In

Java Float intBitsToFloat() method

Java intBitsToFloat() method is a part of the Float class of the java.lang package. This method returns the float value of the integer bits value passed as argument in accordance with the IEEE 754 floating-point 'single format' bit layout.

According to IEEE 754 floating-point representation, a 32 bit and a 64-bit floating number can be represented as below:

32 bit floating point representation

64 bit floating point representation

Syntax:

public static float intBitsToFloat(int bits)  

Parameters:

The parameter passed is the int value whose float value is to be returned.

Returns:

The equivalent float value of the int bits passed.

Example 1:

Here, the integer bit values are converted to its equivalent float value.

import java.lang.Float;

public class StudyTonight
{     
   public static void main(String[] args) 
   {  
       int n1 = 69;
       int n2 = -645;
  
       Float f1=Float.intBitsToFloat(n1);  
       System.out.println("value after conversion = "+f1);
             
       Float f2 = Float.intBitsToFloat(n2);  
       System.out.println("value after conversion = "+f2);
    }  
}  


value after conversion = 9.7E-44
value after conversion = NaN

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.Float;
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);
            int n = sc.nextInt();
            System.out.println(" Float value is = "+ Float.intBitsToFloat(n)); //int bits converted to float 
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }  
   }
}  


Enter value
84
Float value is = 1.18E-43
*******************************
Enter value
-712
Float value is = NaN
******************************
Enter value
0x560
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.