Java >> Tutoriel Java >  >> Tag >> class

Méthode Java Double class toHexString() avec exemple

Méthode double classe toHexString()

  • méthode toHexString() est disponible dans le package java.lang.
  • méthode toHexString() est utilisé pour obtenir la représentation sous forme de chaîne hexadécimale du paramètre donné [valeur] de type à virgule flottante double.
  • méthode toHexString() est une méthode statique, elle est également accessible avec le nom de la classe et si nous essayons d'accéder à la méthode avec l'objet de la classe, nous n'obtiendrons pas non plus d'erreur.
  • méthode toHexString() ne lève pas d'exception au moment de la conversion de chaîne double en chaîne hexadécimale.

Syntaxe :

    public static String toHexString(double value);

Paramètre(s) :

  • valeur double - représente la valeur double à convertir.

Valeur renvoyée :

Le type de retour de cette méthode est chaîne, elle renvoie la chaîne hexadécimale du paramètre donné qui représente la valeur double.

Exemple :

// Java program to demonstrate the example 
// of toHexString(double value) method of Double class

public class ToHexStringOfDoubleClass {
    public static void main(String[] args) {
        // Variables initialization
        double d1 = 10.0;
        double d2 = 20.0;
        double d3 = 30.0;
        double d4 = Double.MAX_VALUE;
        double d5 = Double.MIN_VALUE;

        // Double instance creation
        Double value = new Double(d1);

        // It represents hexadecimal string of the given
        // double type d2 argument
        String s = value.toHexString(d2);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(d2): " + s);

        // It represents hexadecimal string of the given
        // double type d3 argument
        s = value.toHexString(d3);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(d3): " + s);

        // It represents hexadecimal string of the given
        // double type d4 argument
        s = value.toHexString(d4);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(d4): " + s);

        // It represents hexadecimal string of the given
        // double type d5 argument
        s = value.toHexString(d5);

        // Display Hexadecimal String Representation
        System.out.println("value.toHexString(d5): " + s);
    }
}

Sortie

value.toHexString(d2): 0x1.4p4
value.toHexString(d3): 0x1.ep4
value.toHexString(d4): 0x1.fffffffffffffp1023
value.toHexString(d5): 0x0.0000000000001p-1022

Balise Java