BAEL-2995 - possible lossy conversion

This commit is contained in:
Anshul Bansal 2019-06-29 21:37:40 +03:00
parent 0b13fdf96c
commit 6af16b82fb
1 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package com.baeldung.lossyconversion;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
public class ConversionTechniquesUnitTest {
@Test
public void testPrimitiveConversion() {
double doubleNum = 15.6;
int integerNum = (int) doubleNum;
assertEquals(integerNum, 15);
}
@Test
public void testWrapperConversion() {
Double doubleNum = 10.3;
double dbl = doubleNum.doubleValue();
int intgr = (int) dbl;
Integer intNum = Integer.valueOf(intgr);
assertTrue(intNum == 10);
}
@Test
public void testWrapperToPrimitiveConversion() {
Float floatNum = 17.564f;
long longNum = floatNum.longValue();
assertEquals(longNum, 17l);
Double doubleNum = 15.9999;
int intNum = doubleNum.intValue();
assertEquals(intNum, 15);
longNum = Math.round(doubleNum);
assertEquals(longNum, 16);
}
@Test
public void testWrapperToPrimitiveConversionUsingMathRound() {
Double doubleNum = 15.9999;
long longNum = Math.round(doubleNum);
assertEquals(longNum, 16);
}
}