Make Division of Two Integers Result in a Float (#12658)

This commit is contained in:
Kai Yuan 2022-08-30 02:08:50 +02:00 committed by GitHub
parent 83e883f64c
commit 78b7c6dcf8
1 changed files with 26 additions and 0 deletions

View File

@ -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);
}
}