diff --git a/java-basic/DoubleToIntUsingIntValueMethod.java b/java-basic/DoubleToIntUsingIntValueMethod.java new file mode 100644 index 0000000..09f28a5 --- /dev/null +++ b/java-basic/DoubleToIntUsingIntValueMethod.java @@ -0,0 +1,25 @@ +/** + * + * A java program to convert double to int using + * Double.intValue() method + * @author Gaurav Kukade at coderolls.com + * + **/ +public class DoubleToIntUsingIntValueMethod{ + + public static void main(String []args){ + + double doubleValue = 82.14; // 82.14 + + System.out.println("doubleValue: "+doubleValue); + + //create Double wrapper object + Double doubleValueObject = new Double(doubleValue); + + + //typecase double to int + int intValue = doubleValueObject.intValue(); // 82 + + System.out.println("intValue: "+intValue); + } +} diff --git a/java-basic/DoubleToIntUsingRoundMethod.java b/java-basic/DoubleToIntUsingRoundMethod.java new file mode 100644 index 0000000..44df81f --- /dev/null +++ b/java-basic/DoubleToIntUsingRoundMethod.java @@ -0,0 +1,34 @@ +/** + * A java program to convert double to int using + * Math.round() method + * @author Gaurav Kukade at coderolls.com + **/ +public class DoubleToIntUsingRoundMethod{ + + public static void main(String []args){ + + // case 1 + double doubleValue = 82.14; // 82.14 + + System.out.println("doubleValue: "+doubleValue); + + //typecase double to int + int intValue = (int) Math.round(doubleValue); // 82 + + System.out.println("intValue: "+intValue); + + System.out.println(); + + // case 2 + double nextDoubleValue = 82.99; // + + + System.out.println("nextDoubleValue: "+nextDoubleValue); + + // Math.round(nextDoubleValue) returns long value + //typecase long to int + int nextIntValue = (int) Math.round(nextDoubleValue); // 83 + + System.out.println("nextIntValue: "+nextIntValue); + } +} diff --git a/java-basic/DoubleToIntUsingTypecasting.java b/java-basic/DoubleToIntUsingTypecasting.java new file mode 100644 index 0000000..8287654 --- /dev/null +++ b/java-basic/DoubleToIntUsingTypecasting.java @@ -0,0 +1,18 @@ +/** + * A java program to convert double to int using typecasting + * @author Gaurav Kukade at coderolls.com + **/ +public class DoubleToIntUsingTypecasting{ + + public static void main(String []args){ + + double doubleValue = 82.14; // 82.14 + + System.out.println("doubleValue: "+doubleValue); + + //typecase double to int + int intValue = (int) doubleValue; // 82 + + System.out.println("intValue: "+intValue); + } +}