Java double type conversion

In this chapter you will learn:

  1. How to convert double value as byte, double, float, int, long, short
  2. How to convert string value to double
  3. How to convert double value to string
  4. How to get the hexadecimal string representation
  5. How to do bit oriented calculation for double type

Convert double value as byte, double, float, int, long, short

It is easy to convert double value to other primitive types with the following methods.

  • byte byteValue() returns a byte (by casting to a byte).
  • double doubleValue() returns the double value of this Double object.
  • float floatValue() returns the float value of this Double object.
  • int intValue() returns the value of this Double as an int (by casting to type int).
  • long longValue() returns the value of this Double as a long (by casting to type long).
  • short shortValue() returns the value of this Double as a short (by casting to a short).

The following code creates a Double type object and converts to different primitive types.

public class Main {
  public static void main(String[] args) {
    Double doubleObject = new Double("10.01");
    byte b = doubleObject.byteValue();
    System.out.println("byte:"+b);
//  j a va 2  s.c  o  m
    short s = doubleObject.shortValue();
    System.out.println("short:"+s);

    int i = doubleObject.intValue();
    System.out.println("int:"+i);

    float f = doubleObject.floatValue();
    System.out.println("float"+f);

    double d = doubleObject.doubleValue();
    System.out.println("double:"+d);

    long l = doubleObject.longValue();
    System.out.println("long:"+l);
  }
}

The output:

Convert string value to double

Converting string value to double can be done by the following methods.

  • static double parseDouble(String s) returns a double to the value represented by the String.
  • static Double valueOf(double d) returns a Double instance representing the double value.
  • static Double valueOf(String s) returns a Double object holding the double value represented by the argument string s.

The following code tries to convert "1.234" to double type.

public class Main {
  public static void main(String[] args) {
    /* j  a  v  a 2 s . c o  m*/
    System.out.println(Double.valueOf("1.234"));

  }
}

The output:

Double.valueOf can also be used to check if a string is a number.

public class Main {
  public static void main(String[] args) {
    // j a v a2 s.com
    System.out.println(Double.valueOf("1.abc"));

  }
}

The code above produces the following error message:

Convert double value to string

The methods listed below convert double value to string value.

  • String toString() returns a string representation of this Double object.
  • static String toString(double d) returns a string representation of the double argument.

The code below converts double value 1.2 to string.

public class Main {
  public static void main(String[] args) {
    /*from  j a v  a2s  . c  o m*/
    System.out.println(Double.toString(1.2));

  }
}

The output:

Get the hexadecimal string representation

static String toHexString(double d) returns a hexadecimal string representation of the double argument.

public class Main {
  public static void main(String[] args) {
    //from java 2 s.co m
    System.out.println(Double.toHexString(1.2));

  }
}

The output:

Bit oriented calculation for double type

  • static long doubleToLongBits(double value) returns a IEEE 754 floating-point "double format" bit layout.
  • static long doubleToRawLongBits(double value) returns a IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.
  • static double longBitsToDouble(long bits) returns the double value corresponding to a given bit representation.
public class Main {
  public static void main(String[] args) {
    //from  j a v a2  s.  co m
    System.out.println(Double.doubleToLongBits(1.2));

  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is data type casting and converting
  2. What are the conditions for Java's Automatic Conversions
  3. How Java compiler deals with the automatic type conversion and larger type size
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing