how to convert float to int in java code example

Example 1: android studio float to int

int i = Math.round(f); // f a float

Example 2: java convert float to int

int a = (int)float1

Example 3: how to change double to int in java

class Scratch{
    public static void main(String[] args){
        double decimal = 5.7;
        System.out.println( (int) decimal );	//casting
        System.out.println( Math.round(decimal * 10) / (double) 10);	//Math.round()
        System.out.println( decimal % 1);	// modulus
        System.out.println( Integer.parseInt( String.valueOf(decimal).substring(0, String.valueOf(decimal).indexOf(".")))); // .substring()
    }
}

Example 4: float to int in java

Using Math.round() will round the float to the nearest integer.

Example 5: convertir un float en int en java

int bob = (int) 3.14;    \\ bob = 3

Tags:

Java Example