Java double type conversion
In this chapter you will learn:
- How to convert double value as byte, double, float, int, long, short
- How to convert string value to double
- How to convert double value to string
- How to get the hexadecimal string representation
- 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:
- What is data type casting and converting
- What are the conditions for Java's Automatic Conversions
- How Java compiler deals with the automatic type conversion and larger type size