BigDecimal & BigInteger data type implementation (#2181)

This commit is contained in:
Shivang Sarawagi 2017-06-30 21:16:29 +05:30 committed by maibin
parent c2574997f7
commit faeaf0de6a
4 changed files with 80 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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