Reputation: 11673
So far I have
double futurevalue = moneyin * (1+ interest) * year;
Upvotes: 0
Views: 8230
Reputation: 5357
I assume it's the power part of the formula you are having trouble with (multiplying by the year isn't right). For simple compound interest with whole numbers of years you can use the Math.pow() function that's part of the Java SDK.
double futureValue = moneyIn * Math.pow(1 + interest, year)
Upvotes: 3
Reputation: 114767
The Java is correct, the fomular plain wrong. Compund interest is calculated this way:
Kn = K0 * (1 + p/100)n
where n is the number of periods and p is the "interest" per period (annual, if you look at years, p=annual/12
and n=12
if you look at month, have an annual interest as input and want to calculate for a year)
public double compoundInterest(double start, double interest, int periods) {
return start * Math.pow(1 + interest/100, periods);
}
(Note: interest is a percentage value, like 4.2
for 4.2%)
Upvotes: 4