Convert Double to Float in Java
- Using TypeCasting to Convert Double to Float in Java
-
Using
Double.floatValue()
to Convert Double to Float in Java

In this tutorial, we’ll learn how to cast a double value into a float in Java. Double and float types are used to store real or floating-point numbers in Java, but a double data type is more precise than float. Remember that the default data type for floating-point numbers is double.
Using TypeCasting to Convert Double to Float in Java
To define a float type, we must use the suffix f
or F
, whereas it is optional to use the suffix d
or D
for double. The default value of float is 0.0f
, while the default value of double is 0.0d
. By default, float numbers are treated as double in Java.
TypeCasting is the process where we assign a value of one primitive data type into another type. We know that double is a bigger data type than float, so we need to down-cast it. To typecast the function double
into float
, we need to mention the float
keyword in brackets before the decimal value.
We’ll see that the double data type takes more memory to store double-precision numbers and is more accurate in the output. In contrast, the float data type takes less space to store single-precision numbers and gives results formatted up to six decimal points. Hence, we explicitly typecasted the double d
to float f
.
public class JavaDoubleToFloat {
public static void main(String args[]){
double d1 = 1.0;
double d2 = 3.0;
double d = d1/d2;
System.out.println("double d : "+d);
float f = (float) d;
System.out.println("float f : "+f);
}
}
Output:
double d : 0.3333333333333333
float f : 0.33333334
Using Double.floatValue()
to Convert Double to Float in Java
Another way to convert a double value into a float data type in Java is by using the wrapper class Double. This class wraps a primitive data type double
in an object.
Here, we have created a new double object db
, passing the double variable d
in the constructor using the new
keyword; it represents the primitive double argument. We used the method floatValue()
, which returns the value of the double data type as a float after narrowing the primitive conversion.
Using the Narrow Primitive Conversion, we are aware of the fact that we are trying to store a larger data type into a smaller one, so it might lose some information and precision. In the output, we’ll can see that the double is converted into a float and is less precise.
public class JavaDoubleToFloat {
public static void main(String args[]){
double d1 = 1.0;
double d2 = 3.0;
double d = d1/d2;
System.out.println("double d : "+d);
Double db = new Double(d);
float f1 = db.floatValue();
System.out.println("float f1 : "+f1);
}
}
Output:
double d : 0.3333333333333333
float f1 : 0.33333334
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedInRelated Article - Java Double
- Convert Int to Double in Java
- Double in Java
- Compare Doubles in Java
- Convert long to double in Java
- Float and Double Data Type in Java
- Convert Double to String in Java
Related Article - Java Float
- Float and Double Data Type in Java
- Convert Int to Float in Java
- Convert Float to String and String to Float in Java
- Print a Float With 2 Decimal Places in Java