From 193d7b9961910beb5be80487822ea39d210d6419 Mon Sep 17 00:00:00 2001 From: gauravkukade Date: Fri, 5 Mar 2021 00:22:24 +0530 Subject: [PATCH] Add java programs to convert double to int in java --- .../DoubleToIntUsingIntValueMethod.java | 25 ++++++++++++++ java-basic/DoubleToIntUsingRoundMethod.java | 34 +++++++++++++++++++ java-basic/DoubleToIntUsingTypecasting.java | 18 ++++++++++ 3 files changed, 77 insertions(+) create mode 100644 java-basic/DoubleToIntUsingIntValueMethod.java create mode 100644 java-basic/DoubleToIntUsingRoundMethod.java create mode 100644 java-basic/DoubleToIntUsingTypecasting.java 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); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy