Convert float to int in Java

Tags: int float Integer Convert

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

How to cast float value to int value in Java

ConvertFloatToIntExample1.java

public class ConvertFloatToIntExample1 {
    public static void main(String[] args) {
        float value1 = 12.34F;
        int value2 = (int) value1;

        System.out.println("float value: " + value1);
        System.out.println("int value: " + value2);
    }
}
The output is:
float value: 12.34
int value: 12

Using Float.intValue() method to get int value from a Float object

ConvertFloatToIntExample2.java

public class ConvertFloatToIntExample2 {
    public static void main(String[] args) {
        Float value1 = 12.34F;
        int value2 = value1.intValue();

        System.out.println("float value: " + value1);
        System.out.println("int value: " + value2);
    }
}
The output is:
float value: 12.34
int value: 12

Happy Coding 😊