Skip to content

Commit 6c45b08

Browse files
authored
Merge pull request eugenp#5187 from chandra1123/BAEL-2154
Bael 2154
2 parents 9241c94 + 2b67a1b commit 6c45b08

3 files changed

Lines changed: 277 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)