diff --git a/java-numbers/src/test/java/com/baeldung/lossyconversion/ConversionTechniquesUnitTest.java b/java-numbers/src/test/java/com/baeldung/lossyconversion/ConversionTechniquesUnitTest.java new file mode 100644 index 0000000000..9ed714e853 --- /dev/null +++ b/java-numbers/src/test/java/com/baeldung/lossyconversion/ConversionTechniquesUnitTest.java @@ -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); + } +}