forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterest.java
More file actions
26 lines (18 loc) · 707 Bytes
/
Interest.java
File metadata and controls
26 lines (18 loc) · 707 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/***
Compound interest function with ``BigDecimal``
Equivalent in Python:
def compound_interest(principal, rate, periods):
return principal * ((1 + rate) ** periods - 1)
***/
import java.math.BigDecimal;
public class Interest {
static BigDecimal compoundInterest(BigDecimal principal, BigDecimal rate, int periods) {
return principal.multiply(BigDecimal.ONE.add(rate).pow(periods).subtract(BigDecimal.ONE));
}
public static void main(String[] args) {
BigDecimal principal = new BigDecimal(1000);
BigDecimal rate = new BigDecimal("0.06");
int periods = 5;
System.out.println(compoundInterest(principal, rate, periods));
}
}