Skip to content

Commit 568e5f7

Browse files
committed
Calculation with BigDecimal
1 parent 2d62ec1 commit 568e5f7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Chapter8.Interest;
2+
3+
import java.math.BigDecimal;
4+
import java.text.NumberFormat;
5+
6+
// Compound-interest calculations with BigDecimal.
7+
public class Interest {
8+
public static void main(String[] args) {
9+
// initial principal amount before interest
10+
BigDecimal principle = BigDecimal.valueOf(1000.0);
11+
BigDecimal rate = BigDecimal.valueOf(0.05); // interest rate
12+
13+
// display headers
14+
System.out.printf("%s%20s%n", "Year", "Amount on deposit");
15+
16+
// calculate amount on deposit for each of ten years
17+
for(int year = 1; year <= 10; year++) {
18+
// calculate new amount for specified year
19+
BigDecimal amount = principle.multiply(rate.add(BigDecimal.ONE).pow(year));
20+
21+
// display the year and the amount
22+
System.out.printf("%4d%20s%n", year, NumberFormat.getCurrencyInstance().format(amount));
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)