change double to int java code example

Example 1: 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 2: java casting to int

//In java, you can cast to any primitive type by putting (primitiveType)
//before whatever you're casting to.

private static int myInt = (int)myDouble;

Example 3: flutter convert double to int

int calc_ranks(ranks) {
    double multiplier = .5;
    return (multiplier * ranks).round();
}

Example 4: double to int

@SuppressWarnings("deprecation")
int Entero=new Double(345.8).intValue();

Tags:

Java Example