Java Double toHexString Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the toHexString() method of the Double class in Java. This method is used to return the absolute hexadecimal equivalent String of the double value passed. The returned value will be in the format of 0xh.hhhhhhhhhhhhp+d, where h represents a hexadecimal digit and d represents a decimal exponent.

Create a new Java file

Create a new Java file named DoubleToHexString.java using the following command in the terminal.

touch DoubleToHexString.java

Add import statement

Add the following import statement at the beginning of the Java file to import java.lang.Double class.

import java.lang.Double;

Use toHexString() method

In this step, we will use toHexString() method to get the hexadecimal equivalent String of a double value.

public static void main(String[] args) {
    double num = -123.45;
    String hex = Double.toHexString(num);

    System.out.println("Decimal value: " + num);
    System.out.println("Hexadecimal value: " + hex);
}

In the above code, we have created a double variable named num with a value of -123.45. Then we have used toHexString() method to get the hexadecimal equivalent String of num. Finally, we have printed both values using System.out.println() method.

Use NaN value

In this step, we will use toHexString() method for NaN value.

public static void main(String[] args) {
    double num = Double.NaN;
    String hex = Double.toHexString(num);

    System.out.println("Decimal value: " + num);
    System.out.println("Hexadecimal value: " + hex);
}

In the above code, we have used Double.NaN, which represents Not-A-Number (NaN) value, to get the hexadecimal equivalent String. As mentioned earlier, the value "NaN" will be returned for the NaN value. Finally, we have printed both values using System.out.println() method.

User input

In this step, we will get input from the user and then use toHexString() method to get the hexadecimal equivalent String of the input value.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a decimal value: ");
    double num = sc.nextDouble();

    String hex = Double.toHexString(num);

    System.out.println("Decimal value: " + num);
    System.out.println("Hexadecimal value: " + hex);
}

In the above code, we have used Scanner class to get the input value from the user. We have then used toHexString() to get the hexadecimal equivalent String of the input value. Finally, we have printed both values using System.out.println() method.

Compile and run the code

Compile the code using the following command:

javac DoubleToHexString.java

Run the code using the following command:

java DoubleToHexString

You will see output similar to this:

Enter a decimal value: 123.45
Decimal value: 123.45
Hexadecimal value: 0x1.edd2f1a9fbe77p6

Summary

In this lab, you have learned how to use the toHexString() method of the Double class to get the hexadecimal equivalent String of a given double value. You have also learned how to handle NaN value using this method.

Other Java Tutorials you may like