From 6913b18b13ab9a6cfb15d1472e8c06582922f53c Mon Sep 17 00:00:00 2001 From: "sreekanth.nair" Date: Sun, 1 Dec 2019 17:32:21 +0530 Subject: [PATCH] Double to Integer Casting --- .../com/baeldung/casting/DoubleToInteger.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 java-numbers/src/main/java/com/baeldung/casting/DoubleToInteger.java diff --git a/java-numbers/src/main/java/com/baeldung/casting/DoubleToInteger.java b/java-numbers/src/main/java/com/baeldung/casting/DoubleToInteger.java new file mode 100644 index 0000000000..78af7f9f9d --- /dev/null +++ b/java-numbers/src/main/java/com/baeldung/casting/DoubleToInteger.java @@ -0,0 +1,42 @@ +package com.baeldung.casting; + +public class DoubleToInteger { + + static Double value = 99999.999; + + public static void main(String[] args) { + + System.out.println(usingIntValue(value)); + + System.out.println(usingMathRound(value)); + System.out.println(usingMathCeil(value)); + + System.out.println(usingMathFloor(value)); + System.out.println(usingMathAbs(value)); + } + + public static Integer usingIntValue(Double value) { + return value.intValue(); + } + + public static Integer usingMathRound(Double value) { + return (int) Math.round(value); + } + + public static Integer usingMathCeil(Double value) { + return (int) Math.ceil(value); + } + + public static Integer usingMathFloor(Double value) { + return (int) Math.floor(value); + } + + public static Integer usingMathAbs(Double value) { + return (int) Math.abs(value); + } + + public static Integer usingCast(Double value) { + return (int) value.doubleValue(); + } + +}