Convert float to long in Java

Tags: long float Convert

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

How to cast float to long value in Java

ConvertFloatToLongExample1.java

public class ConvertFloatToLongExample1 {
    public static void main(String[] args) {
        float value1 = 56.78F;
        long value2 = (long) value1;

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

Using Float.longValue() method to get long value from a Float object

ConvertFloatToLongExample2.java

public class ConvertFloatToLongExample2 {
    public static void main(String[] args) {
        Float value1 = Float.valueOf(99.09F);
        long value2 = value1.longValue();

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

Happy Coding 😊