From 78b7c6dcf87187d8e260e3c3b962d3e36bc80b1f Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Tue, 30 Aug 2022 02:08:50 +0200 Subject: [PATCH] Make Division of Two Integers Result in a Float (#12658) --- .../intdivision/IntegerDivisionUnitTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intdivision/IntegerDivisionUnitTest.java diff --git a/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intdivision/IntegerDivisionUnitTest.java b/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intdivision/IntegerDivisionUnitTest.java new file mode 100644 index 0000000000..46527575e9 --- /dev/null +++ b/core-java-modules/core-java-numbers-5/src/test/java/com/baeldung/intdivision/IntegerDivisionUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.intdivision; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class IntegerDivisionUnitTest { + @Test + void givenTwoInt_whenExecDivision_shouldGetInteger() { + int i = 10 / 4; + assertEquals(2, i); + + float x = 10 / 4; + assertEquals(2, x); + } + + @Test + void givenTwoInt_whenCastAnyoneToFloat_shouldGetFloatResult() { + float x = (float) 10 / 4; + assertEquals(2.5, x); + + float y = 10 / (float) 8; + assertEquals(1.25, y); + } + +}