Convert double to short in Java
In this Java core tutorial, we learn how to convert double value into short value in Java via different solutions.
How to cast double to short value
In Java to convert a double value to short value we can cast the double variable to short variable as following Java example code.
ConvertDoubleToShortExample1.java
public class ConvertDoubleToShortExample1 {
public static void main(String[] args) {
double value1 = 98.76;
short value2 = (short)value1;
System.out.println("double value: " + value1);
System.out.println("short value: " + value2);
}
}
double value: 98.76
short value: 98
How to use Double.shortValue() method to get short value of a Double value
With a Double object in Java we can use the Double.shortValue() to return short value of a given Double object.
ConvertDoubleToShortExample2.java
public class ConvertDoubleToShortExample2 {
public static void main(String[] args) {
Double value1 = Double.valueOf(12.34);
short value2 = value1.shortValue();
System.out.println("double value: " + value1);
System.out.println("short value: " + value2);
}
}
double value: 12.34
short value: 12
Happy Coding 😊
Related Articles
Convert short to double in Java
Convert double to String in Java
Convert double to float in Java
Convert double to long in Java