Java.lang Package Classes

Java Float - toHexString() Method



The java.lang.Float.toHexString() method returns a hexadecimal string representation of the float argument. Some examples are shown below:

Floating-point ValueHexadecimal String
1.00x1.0p0
-1.0-0x1.0p0
2.00x1.0p1
3.00x1.8p1
0.50x1.0p-1
0.250x1.0p-2
Float.MAX_VALUE0x1.fffffep127
Minimum Normal Value0x1.0p-126
Maximum Subnormal Value0x0.fffffep-126
Float.MIN_VALUE0x0.000002p-126

Syntax

public static String toHexString(float f)

Parameters

f Specify the float to be converted.

Return Value

Returns a hex string representation of the argument.

Exception

NA.

Example:

In the example below, the java.lang.Float.toHexString() method returns a hexadecimal string representing the given float argument.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating float values
    float x = 5.2f;
    float y = -5.2f;

    //printing the hexadecimal string for x
    System.out.print("Hexadecimal string for x is: " );
    System.out.println(Float.toHexString(x));    

    //printing the hexadecimal string for y
    System.out.print("Hexadecimal string for y is: " );
    System.out.println(Float.toHexString(y)); 
  }
}

The output of the above code will be:

Hexadecimal string for x is: 0x1.4cccccp2
Hexadecimal string for y is: -0x1.4cccccp2

❮ Java.lang - Float