Skip to content

Commit b941475

Browse files
committed
BAEL-2154 - BigDecimal and BigInteger article.
1 parent 04c255c commit b941475

3 files changed

Lines changed: 279 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.math.BigDecimal;
2+
import java.math.RoundingMode;
3+
4+
public class BigDecimalDemo {
5+
6+
/** Calculate total amount to be paid for an item rounded to cents..
7+
* @param quantity
8+
* @param unitPrice
9+
* @param discountRate
10+
* @param taxRate
11+
* @return
12+
*/
13+
public static BigDecimal calculateTotalAmount(BigDecimal quantity,
14+
BigDecimal unitPrice, BigDecimal discountRate, BigDecimal taxRate) {
15+
BigDecimal amount = quantity.multiply(unitPrice);
16+
BigDecimal discount = amount.multiply(discountRate);
17+
BigDecimal discountedAmount = amount.subtract(discount);
18+
BigDecimal tax = taxRate.multiply(taxRate);
19+
BigDecimal total = discountedAmount.add(tax);
20+
21+
// round to 2 decimal places using HALF_EVEN
22+
BigDecimal roundedTotal = total.setScale(2, RoundingMode.HALF_EVEN);
23+
24+
return roundedTotal;
25+
}
26+
27+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import static org.junit.Assert.assertFalse;
2+
import static org.junit.Assert.assertNotEquals;
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
8+
import java.math.MathContext;
9+
import java.math.RoundingMode;
10+
import java.util.Random;
11+
12+
import org.junit.jupiter.api.Test;
13+
14+
public class BigDecimalDemoUnitTest {
15+
16+
@Test
17+
void test() {
18+
System.out.println(BigDecimal.ONE
19+
.divide(BigDecimal.valueOf(3), 4, RoundingMode.CEILING)
20+
.precision());
21+
}
22+
23+
@Test
24+
void whenBigDecimalCreated_thenValueMatches() {
25+
BigDecimal bdFromString = new BigDecimal("0.1");
26+
BigDecimal bdFromCharArray = new BigDecimal(
27+
new char[] { '3', '.', '1', '6', '1', '5' });
28+
BigDecimal bdlFromInt = new BigDecimal(42);
29+
BigDecimal bdFromLong = new BigDecimal(123412345678901L);
30+
BigInteger bigInteger = BigInteger.probablePrime(100, new Random());
31+
BigDecimal bdFromBigInteger = new BigDecimal(bigInteger);
32+
33+
assertEquals("0.1", bdFromString.toString());
34+
assertEquals("3.1615", bdFromCharArray.toString());
35+
assertEquals("42", bdlFromInt.toString());
36+
assertEquals("123412345678901", bdFromLong.toString());
37+
assertEquals(bigInteger.toString(), bdFromBigInteger.toString());
38+
}
39+
40+
@Test
41+
void whenBigDecimalCreatedFromDouble_thenValueMayNotMatch() {
42+
BigDecimal bdFromDouble = new BigDecimal(0.1d);
43+
assertNotEquals("0.1", bdFromDouble.toString());
44+
}
45+
46+
@Test
47+
void whenBigDecimalCreatedUsingValueOf_thenValueMatches() {
48+
BigDecimal bdFromLong1 = BigDecimal.valueOf(123412345678901L);
49+
BigDecimal bdFromLong2 = BigDecimal.valueOf(123412345678901L, 2);
50+
BigDecimal bdFromDouble = BigDecimal.valueOf(0.1d);
51+
52+
assertEquals("123412345678901", bdFromLong1.toString());
53+
assertEquals("1234123456789.01", bdFromLong2.toString());
54+
assertEquals("0.1", bdFromDouble.toString());
55+
}
56+
57+
@Test
58+
void whenEqualsCalled_thenSizeAndScaleMatched() {
59+
BigDecimal bd1 = new BigDecimal("1.0");
60+
BigDecimal bd2 = new BigDecimal("1.00");
61+
62+
assertFalse(bd1.equals(bd2));
63+
}
64+
65+
@Test
66+
void whenComparingBigDecimals_thenExpectedResult() {
67+
BigDecimal bd1 = new BigDecimal("1.0");
68+
BigDecimal bd2 = new BigDecimal("1.00");
69+
BigDecimal bd3 = new BigDecimal("2.0");
70+
71+
assertTrue(bd1.compareTo(bd3) < 0);
72+
assertTrue(bd3.compareTo(bd1) > 0);
73+
assertTrue(bd1.compareTo(bd2) == 0);
74+
assertTrue(bd1.compareTo(bd3) <= 0);
75+
assertTrue(bd1.compareTo(bd2) >= 0);
76+
assertTrue(bd1.compareTo(bd3) != 0);
77+
}
78+
79+
@Test
80+
void whenPerformingArithmetic_thenExpectedResult() {
81+
BigDecimal bd1 = new BigDecimal("4.0");
82+
BigDecimal bd2 = new BigDecimal("2.0");
83+
84+
BigDecimal sum = bd1.add(bd2);
85+
BigDecimal difference = bd1.subtract(bd2);
86+
BigDecimal quotient = bd1.divide(bd2);
87+
BigDecimal product = bd1.multiply(bd2);
88+
89+
assertTrue(sum.compareTo(new BigDecimal("6.0")) == 0);
90+
assertTrue(difference.compareTo(new BigDecimal("2.0")) == 0);
91+
assertTrue(quotient.compareTo(new BigDecimal("2.0")) == 0);
92+
assertTrue(product.compareTo(new BigDecimal("8.0")) == 0);
93+
}
94+
95+
@Test
96+
void whenGettingAttributes_thenExpectedResult() {
97+
BigDecimal bd = new BigDecimal("-12345.6789");
98+
99+
assertEquals(9, bd.precision());
100+
assertEquals(4, bd.scale());
101+
assertEquals(-1, bd.signum());
102+
}
103+
104+
@Test
105+
void whenRoundingDecimal_thenExpectedResult() {
106+
BigDecimal bd = new BigDecimal("2.5");
107+
// Round to 1 digit using HALF_EVEN
108+
BigDecimal rounded = bd
109+
.round(new MathContext(1, RoundingMode.HALF_EVEN));
110+
111+
assertEquals("2", rounded.toString());
112+
}
113+
114+
@Test
115+
void givenPurchaseTxn_whenCalculatingTotalAmount_thenExpectedResult() {
116+
BigDecimal quantity = new BigDecimal("4.5");
117+
BigDecimal unitPrice = new BigDecimal("2.69");
118+
BigDecimal discountRate = new BigDecimal("0.10");
119+
BigDecimal taxRate = new BigDecimal("0.0725");
120+
121+
BigDecimal amountToBePaid = BigDecimalDemo
122+
.calculateTotalAmount(quantity, unitPrice, discountRate, taxRate);
123+
124+
assertEquals("10.90", amountToBePaid.toString());
125+
}
126+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import static org.junit.Assert.assertEquals;
2+
import static org.junit.Assert.assertTrue;
3+
4+
import java.math.BigInteger;
5+
import java.util.Random;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class BigIntegerDemoUnitTest {
10+
11+
@Test
12+
void whenBigIntegerCreatedFromConstructor_thenExpectedResult() {
13+
BigInteger biFromString = new BigInteger("1234567890987654321");
14+
BigInteger biFromByteArray = new BigInteger(
15+
new byte[] { 64, 64, 64, 64, 64, 64 });
16+
BigInteger biFromSignMagnitude = new BigInteger(-1,
17+
new byte[] { 64, 64, 64, 64, 64, 64 });
18+
19+
assertEquals("1234567890987654321", biFromString.toString());
20+
assertEquals("70644700037184", biFromByteArray.toString());
21+
assertEquals("-70644700037184", biFromSignMagnitude.toString());
22+
}
23+
24+
@Test
25+
void whenLongConvertedToBigInteger_thenValueMatches() {
26+
BigInteger bi = BigInteger.valueOf(2305843009213693951L);
27+
28+
assertEquals("2305843009213693951", bi.toString());
29+
}
30+
31+
@Test
32+
void givenBigIntegers_whentCompared_thenExpectedResult() {
33+
BigInteger i = new BigInteger("123456789012345678901234567890");
34+
BigInteger j = new BigInteger("123456789012345678901234567891");
35+
BigInteger k = new BigInteger("123456789012345678901234567892");
36+
37+
assertTrue(i.compareTo(i) == 0);
38+
assertTrue(j.compareTo(i) > 0);
39+
assertTrue(j.compareTo(k) < 0);
40+
}
41+
42+
@Test
43+
void givenBigIntegers_whenPerformingArithmetic_thenExpectedResult() {
44+
BigInteger i = new BigInteger("4");
45+
BigInteger j = new BigInteger("2");
46+
47+
BigInteger sum = i.add(j);
48+
BigInteger difference = i.subtract(j);
49+
BigInteger quotient = i.divide(j);
50+
BigInteger product = i.multiply(j);
51+
52+
assertEquals(new BigInteger("6"), sum);
53+
assertEquals(new BigInteger("2"), difference);
54+
assertEquals(new BigInteger("2"), quotient);
55+
assertEquals(new BigInteger("8"), product);
56+
}
57+
58+
@Test
59+
void givenBigIntegers_whenPerformingBitOperations_thenExpectedResult() {
60+
BigInteger i = new BigInteger("17");
61+
BigInteger j = new BigInteger("7");
62+
63+
BigInteger and = i.and(j);
64+
BigInteger or = i.or(j);
65+
BigInteger not = j.not();
66+
BigInteger xor = i.xor(j);
67+
BigInteger andNot = i.andNot(j);
68+
BigInteger shiftLeft = i.shiftLeft(1);
69+
BigInteger shiftRight = i.shiftRight(1);
70+
71+
assertEquals(new BigInteger("1"), and);
72+
assertEquals(new BigInteger("23"), or);
73+
assertEquals(new BigInteger("-8"), not);
74+
assertEquals(new BigInteger("22"), xor);
75+
assertEquals(new BigInteger("16"), andNot);
76+
assertEquals(new BigInteger("34"), shiftLeft);
77+
assertEquals(new BigInteger("8"), shiftRight);
78+
}
79+
80+
@Test
81+
void givenBigIntegers_whenPerformingBitManipulations_thenExpectedResult() {
82+
BigInteger i = new BigInteger("1018");
83+
84+
int bitCount = i.bitCount();
85+
int bitLength = i.bitLength();
86+
int getLowestSetBit = i.getLowestSetBit();
87+
boolean testBit3 = i.testBit(3);
88+
BigInteger setBit12 = i.setBit(12);
89+
BigInteger flipBit0 = i.flipBit(0);
90+
BigInteger clearBit3 = i.clearBit(3);
91+
92+
assertEquals(8, bitCount);
93+
assertEquals(10, bitLength);
94+
assertEquals(1, getLowestSetBit);
95+
assertEquals(true, testBit3);
96+
assertEquals(new BigInteger("5114"), setBit12);
97+
assertEquals(new BigInteger("1019"), flipBit0);
98+
assertEquals(new BigInteger("1010"), clearBit3);
99+
}
100+
101+
@Test
102+
void givenBigIntegers_whenModularCalculation_thenExpectedResult() {
103+
BigInteger i = new BigInteger("31");
104+
BigInteger j = new BigInteger("24");
105+
BigInteger k = new BigInteger("16");
106+
107+
BigInteger gcd = j.gcd(k);
108+
BigInteger multiplyAndmod = j.multiply(k)
109+
.mod(i);
110+
BigInteger modInverse = j.modInverse(i);
111+
BigInteger modPow = j.modPow(k, i);
112+
113+
assertEquals(new BigInteger("8"), gcd);
114+
assertEquals(new BigInteger("12"), multiplyAndmod);
115+
assertEquals(new BigInteger("22"), modInverse);
116+
assertEquals(new BigInteger("7"), modPow);
117+
}
118+
119+
@Test
120+
void givenBigIntegers_whenPrimeOperations_thenExpectedResult() {
121+
BigInteger i = BigInteger.probablePrime(100, new Random());
122+
123+
boolean isProbablePrime = i.isProbablePrime(1000);
124+
assertEquals(true, isProbablePrime);
125+
}
126+
}

0 commit comments

Comments
 (0)