Convert Double to Float in Java

Rupam Yadav Jan 30, 2023 May 31, 2021
  1. Using TypeCasting to Convert Double to Float in Java
  2. Using Double.floatValue() to Convert Double to Float in Java
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
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Double

Related Article - Java Float