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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117632{{"`Java Double toHexString Method`"}} java/class_methods -.-> lab-117632{{"`Java Double toHexString Method`"}} java/modifiers -.-> lab-117632{{"`Java Double toHexString Method`"}} java/oop -.-> lab-117632{{"`Java Double toHexString Method`"}} java/packages_api -.-> lab-117632{{"`Java Double toHexString Method`"}} java/user_input -.-> lab-117632{{"`Java Double toHexString Method`"}} java/wrapper_classes -.-> lab-117632{{"`Java Double toHexString Method`"}} java/identifier -.-> lab-117632{{"`Java Double toHexString Method`"}} java/arrays -.-> lab-117632{{"`Java Double toHexString Method`"}} java/data_types -.-> lab-117632{{"`Java Double toHexString Method`"}} java/operators -.-> lab-117632{{"`Java Double toHexString Method`"}} java/output -.-> lab-117632{{"`Java Double toHexString Method`"}} java/strings -.-> lab-117632{{"`Java Double toHexString Method`"}} java/variables -.-> lab-117632{{"`Java Double toHexString Method`"}} java/string_methods -.-> lab-117632{{"`Java Double toHexString Method`"}} java/system_methods -.-> lab-117632{{"`Java Double toHexString Method`"}} end

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