Convert double to float in Java
In this Java core tutorial, we learn how to convert double value into float value in Java via different solutions.
How to cast double to float value
In Java we can cast a double value into a float value as the following Java program.
ConvertDoubleToFloatExample1.java
public class ConvertDoubleToFloatExample1 {
public static void main(String[] args) {
double value1 = 123.45;
float value2 = (float)value1;
System.out.println("double value:" + value1);
System.out.println("float value:" + value2);
}
}
double value:123.45
float value:123.45
Using Double.floatValue() method to return float value from a Double object
With a Double object in Java we can use the Double.floatValue() method to return float value as below.
ConvertDoubleToFloatExample2.java
public class ConvertDoubleToFloatExample2 {
public static void main(String[] args) {
Double value1 = Double.valueOf(99.9);
float value2 = value1.floatValue();
System.out.println("double value:" + value1);
System.out.println("float value:" + value2);
}
}
double value:99.9
float value:99.9
Happy Coding 😊
Related Articles
Convert float to double in Java
Convert double to String in Java
Convert double to long in Java
Convert double to short in Java