convert double to int code example

Example 1: javascript convert float to int

function float2int (value) {
    return value | 0;
}

float2int(3.75); //3 - always just truncates decimals

//other options
Math.floor( 3.75 );//3 - goes to floor , note (-3.75 = -4)
Math.ceil( 3.75 ); //4 - goes to ceiling, note (-3.75 = -3)
Math.round( 3.75 );//4

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: 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 4: convert double into integer

class DoubleToInt {
    public static void main( String args[] ) {
        double DoubleValue = 3.6987;
        int IntValue = (int) DoubleValue;
        System.out.println(DoubleValue + " is now " + IntValue);
    }
}

Example 5: convert double to int java

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

Example 6: double to int

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