Convert float to double in Java
In this Java core tutorial, we learn how to convert float value into double value in Java via different solutions.
Assign float variable to double variable
In Java, to convert a float value to double value we can easily assign float variable to double variable as following Java program.
ConvertFloatToDoubleExample1.java
public class ConvertFloatToDoubleExample1 {
public static void main(String[] args) {
float value1 = 9.8F;
double value2 = value1;
System.out.println("float value: " + value1);
System.out.println("double value: " + value2);
}
}
float value: 9.8
double value: 9.800000190734863
Using Double.valueOf() method to instantiate Double from float value
We can use the Double.valueOf() method to initialize a double value from a given float value as Java code below.
ConvertFloatToDoubleExample2.java
public class ConvertFloatToDoubleExample2 {
public static void main(String[] args) {
float value1 = 20.09F;
double value2 = Double.valueOf(value1);
System.out.println("float value: " + value1);
System.out.println("double value: " + value2);
}
}
float value: 20.09
double value: 20.09000015258789
Using Float.doubleValue() method to get double value of a Float object
With a Float object in Java we can use Float.doubleValue() method to return double value of it.
ConvertFloatToDoubleExample3.java
public class ConvertFloatToDoubleExample3 {
public static void main(String[] args) {
Float value1 = Float.valueOf(20.05F);
double value2 = value1.doubleValue();
System.out.println("float value: " + value1);
System.out.println("double value: " + value2);
}
}
float value: 20.05
double value: 20.049999237060547
Happy Coding 😊
Related Articles
Convert double to float in Java
Convert float to String in Java