Close

Java - Floating Point To Integral Representation

[Updated: May 23, 2018, Created: May 21, 2018]

Java Number Classes Java 

Float class has following methods which can be used to convert float to int bits and vice-versa.

public static int floatToIntBits(float value)
public static float intBitsToFloat(int bits)
package com.logicbig.example;

public class FloatToIntBitsExample {
  public static void main(String[] args) {
      int i = Float.floatToIntBits(1.5f);
      System.out.println(i);

      float f = Float.intBitsToFloat(i);
      System.out.println(f);

  }
}
1069547520
1.5



Double class has the similar methods:

public static long doubleToLongBits(double value)
public static double longBitsToDouble(long bits)
package com.logicbig.example;

public class DoubleToLongBitsExample {
  public static void main(String[] args) {
      long l = Double.doubleToLongBits(1.5d);
      System.out.println(l);

      double d = Double.longBitsToDouble(l);
      System.out.println(d);
  }
}
4609434218613702656
1.5

See Also