Convert float to byte in Java
In this Java core tutorial, we learn how to convert float value into byte value in Java via different solutions.
Table of contents
- How to cast float to byte value in Java
- Using Float.byteValue() method to get byte value from a Float object
How to cast float to byte value in Java
ConvertFloatToByteExample1.java
public class ConvertFloatToByteExample1 {
public static void main(String[] args) {
float value1 = 56.78F;
byte value2 = (byte) value1;
System.out.println("float value: " + value1);
System.out.println("byte value: " + value2);
}
}
float value: 56.78
byte value: 56
Using Float.byteValue() method to get byte value from a Float object
ConvertFloatToByteExample2.java
public class ConvertFloatToByteExample2 {
public static void main(String[] args) {
Float value1 = 23.45F;
byte value2 = value1.byteValue();
System.out.println("float value: " + value1);
System.out.println("byte value: " + value2);
}
}
float value: 23.45
byte value: 23
Happy Coding 😊
Related Articles
Convert float to String in Java