Signup/Sign In

Java Double toHexString() Method

Java toHexString() method is the part of the Double class of the java.lang package. This method is used to return the absolute hexadecimal equivalent String of the double value passed.

It must be noted that the value "NaN" will be returned for the Not-A-Number(NaN) value.


public static String toHexString(double d)  


The parameter passed is the double value of which the string representation of the equivalent hexadecimal is to be returned.


Returns the String representation of the double value passed as an equivalent hexadecimal value.

Example 1:

Here, a positive and a negative number is taken for a better understanding of the method.

import java.lang.Double;

public class StudyTonight
    public static void main(String[] args) 
        double i = 132.67; 
        double j = -52.45;
        System.out.println("Actual number is = " + i);  
        System.out.println("Hexadecimal equivalent is = " + Double.toHexString(i)); //returns the double value in hexadecimal string
        System.out.println("Actual number is = " + j);  
        System.out.println("Hexadecimal equivalent is = " + Double.toHexString(j)); //returns the double value in hexadecimal string

Actual number is = 132.67
Hexadecimal equivalent is = 0x1.09570a3d70a3dp7
Actual number is = -52.45
Hexadecimal equivalent is = -0x1.a39999999999ap5

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.util.Scanner;  
public class StudyTonight
    public static void main(String[] args) 
          System.out.print("Enter the Number = ");  
          Scanner sc = new Scanner(;  
          double i = sc.nextDouble();  
          System.out.println("Actual Number is  = " + i);  
          System.out.println("Hexadecimal representation is = " + Double.toHexString(i)); //returns the double value in hexadecimal string 
        catch(Exception e)
          System.out.println("Invalid Number");

Enter the Number = 89.77
Actual Number is = 89.77
Hexadecimal representation is = 0x1.67147ae147ae1p6
Enter the Number = -72.96
Actual Number is = -72.96
Hexadecimal representation is = -0x1.23d70a3d70a3dp6
Enter the Number = 0x56.98
Invalid Number

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.