Java converts a ints to doubles automatically but i want to convert the double for comparing. If i divide an int numerator by an int denominator, like 2/5, and wanted the result to be in a decimal, .40, how would you turn it into a double?
Java converts a ints to doubles automatically but i want to convert the double for comparing. If i divide an int numerator by an int denominator, like 2/5, and wanted the result to be in a decimal, .40, how would you turn it into a double?
Double.intValue() converts a Double to an int. Double.intValue() converts a Double to an int. I don't think there's any way to go directly from a double to an int. Java does not let us assign a floating-point value (float or double) directly to an integer variable (byte, short, int or long).
Convert Double to Integer in Java :
Code:Import java.lang.Math.*; public class java { public static void main(String avg[]) { int d=0; try { Double e=(940.0/1000.0)*100; d=e.intValue(); }catch(Exception f){} System.out.println(d); } }
Code:public class javaintdouble { public static void main(String[] avg){ double intdbl = 430.5; //Type cast double to int int j = (int)intdbl; System.out.println(j); } }
Bookmarks