Convert long to float in Java

Tags: long float Convert

In this Java core tutorial, we learn how to convert long value into float value in Java via different solutions.

Assign long variable to float variable in Java

ConvertLongToFloatExample1.java

public class ConvertLongToFloatExample1 {
    public static void main(String[] args) {
        long value1 = 100;
        float value2 = value1;

        System.out.println("long value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
long value: 100
float value: 100.0

Using Float.valueOf() to instantiate float from a long value

ConvertLongToFloatExample2.java

public class ConvertLongToFloatExample2 {
    public static void main(String[] args) {
        long value1 = 1898;
        float value2 = Float.valueOf(value1);

        System.out.println("long value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
long value: 1898
float value: 1898.0

Using Long.floatValue() method to get float value from a Long object

ConvertLongToFloatExample3.java

public class ConvertLongToFloatExample3 {
    public static void main(String[] args) {
        Long value1 = Long.valueOf(10000);
        float value2 = value1.floatValue();

        System.out.println("long value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
long value: 10000
float value: 10000.0

Happy Coding 😊