cast a double to an int java code example

Example 1: int to double java

Double d= new Double(i);//first way  
Double d2=Double.valueOf(i);//second way

Example 2: 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 3: double to int

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

Example 4: int to double java

double d=i;

Example 5: convert double to int java

double data = 3452.345
int value = (int)data;

Tags:

Java Example