Convert double to byte in Java
In this Java core tutorial, we learn how to convert double value into byte value in Java via different solutions.
How to cast double to byte value
ConvertDoubleToByteExample1.java
public class ConvertDoubleToByteExample1 {
public static void main(String[] args) {
double value1 = 10.20;
byte value2 = (byte)value1;
System.out.println("double value: " + value1);
System.out.println("byte value: " + value2);
}
}
double value: 10.2
byte value: 10
How to use Double.byteValue() method to return value of Double as a byte
ConvertDoubleToByteExample2.java
public class ConvertDoubleToByteExample2 {
public static void main(String[] args) {
double value1 = 12.34;
Double doubleValue = value1;
byte value2 = doubleValue.byteValue();
System.out.println("double value: " + value1);
System.out.println("byte value: " + value2);
}
}
double value: 12.34
byte value: 12
Happy Coding 😊