diff --git a/core-java/src/main/java/com/baeldung/maths/BigDecimalImpl.java b/core-java/src/main/java/com/baeldung/maths/BigDecimalImpl.java new file mode 100644 index 0000000000..1472dd7d6d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/maths/BigDecimalImpl.java @@ -0,0 +1,18 @@ +package com.baeldung.maths; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class BigDecimalImpl { + + public static void main(String[] args) { + + BigDecimal serviceTax = new BigDecimal("56.0084578639"); + serviceTax = serviceTax.setScale(2, RoundingMode.CEILING); + + BigDecimal entertainmentTax = new BigDecimal("23.00689"); + entertainmentTax = entertainmentTax.setScale(2, RoundingMode.FLOOR); + + BigDecimal totalTax = serviceTax.add(entertainmentTax); + } +} diff --git a/core-java/src/main/java/com/baeldung/maths/BigIntegerImpl.java b/core-java/src/main/java/com/baeldung/maths/BigIntegerImpl.java new file mode 100644 index 0000000000..dc509429f9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/maths/BigIntegerImpl.java @@ -0,0 +1,16 @@ +package com.baeldung.maths; + +import java.math.BigInteger; + +public class BigIntegerImpl { + + public static void main(String[] args) { + + BigInteger numStarsMilkyWay = new BigInteger("8731409320171337804361260816606476"); + BigInteger numStarsAndromeda = new BigInteger("5379309320171337804361260816606476"); + + BigInteger totalStars = numStarsMilkyWay.add(numStarsAndromeda); + + } + +} diff --git a/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java b/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java new file mode 100644 index 0000000000..973b987224 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/maths/BigDecimalImplTest.java @@ -0,0 +1,24 @@ +package com.baeldung.maths; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import org.junit.Assert; +import org.junit.Test; + +public class BigDecimalImplTest { + + @Test + public void givenBigDecimalNumbers_whenAddedTogether_thenGetExpectedResult() { + BigDecimal serviceTax = new BigDecimal("56.0084578639"); + serviceTax = serviceTax.setScale(2, RoundingMode.CEILING); + + BigDecimal entertainmentTax = new BigDecimal("23.00689"); + entertainmentTax = entertainmentTax.setScale(2, RoundingMode.FLOOR); + + BigDecimal totalTax = serviceTax.add(entertainmentTax); + BigDecimal result = BigDecimal.valueOf(79.01); + + Assert.assertEquals(result, totalTax); + + } +} diff --git a/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java b/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java new file mode 100644 index 0000000000..040c595065 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/maths/BigIntegerImplTest.java @@ -0,0 +1,22 @@ +package com.baeldung.maths; + +import java.math.BigInteger; + +import org.junit.Assert; + +import org.junit.Test; + +public class BigIntegerImplTest { + + @Test + public void givenBigIntegerNumbers_whenAddedTogether_thenGetExpectedResult() { + BigInteger numStarsMilkyWay = new BigInteger("8731409320171337804361260816606476"); + BigInteger numStarsAndromeda = new BigInteger("5379309320171337804361260816606476"); + + BigInteger totalStars = numStarsMilkyWay.add(numStarsAndromeda); + BigInteger result = new BigInteger("14110718640342675608722521633212952"); + + Assert.assertEquals(result, totalStars); + } + +}