I am simply confused on how to convert the equation below for bigDecimal objects. I have already tried this, and this and the output is really weird once I call the method.
The first block of code is what I'm trying to convert into BigDecimal arithmetic.
public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months)
{
double futureValue = 0;
for (int i = 1; i <= months; i++)
{
futureValue =
(futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
// System.out.println("Month: " + i + " FutureValue: " + futureValue);
}
return futureValue;
}
}
My attempt at this is as follows:
public static BigDecimal calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months)
{
BigDecimal futureValue = new BigDecimal(0.0);
BigDecimal montlyInvestmentDecimal = new BigDecimal(monthlyInvestment);
BigDecimal montlyInterestRateDecimal = new BigDecimal(monthlyInterestRate);
BigDecimal one = new BigDecimal(1.0);
for (int i = 1; i <= months; i++)
{
futureValue = (futureValue.add(futureValue)).multiply
(one.add(montlyInterestRateDecimal));
System.out.println("Month: " + i + " FutureValue: " + futureValue);
}
return futureValue;
}
Output:
Welcome to the Future Value Calculator
DATA ENTRY
Enter monthly investment: 1
Enter yearly interest rate: .01
Enter number of years: 3
Month: 1 FutureValue: 0E-66
Month: 2 FutureValue: 0E-132
Month: 3 FutureValue: 0E-198
Month: 4 FutureValue: 0E-264
Month: 5 FutureValue: 0E-330
Month: 6 FutureValue: 0E-396
Month: 7 FutureValue: 0E-462
Month: 8 FutureValue: 0E-528
Month: 9 FutureValue: 0E-594
Month: 10 FutureValue: 0E-660
Month: 11 FutureValue: 0E-726
Month: 12 FutureValue: 0E-792
Month: 13 FutureValue: 0E-858
Month: 14 FutureValue: 0E-924
Month: 15 FutureValue: 0E-990
Month: 16 FutureValue: 0E-1056
Month: 17 FutureValue: 0E-1122
Month: 18 FutureValue: 0E-1188
Month: 19 FutureValue: 0E-1254
Month: 20 FutureValue: 0E-1320
Month: 21 FutureValue: 0E-1386
Month: 22 FutureValue: 0E-1452
Month: 23 FutureValue: 0E-1518
Month: 24 FutureValue: 0E-1584
Month: 25 FutureValue: 0E-1650
Month: 26 FutureValue: 0E-1716
Month: 27 FutureValue: 0E-1782
Month: 28 FutureValue: 0E-1848
Month: 29 FutureValue: 0E-1914
Month: 30 FutureValue: 0E-1980
Month: 31 FutureValue: 0E-2046
Month: 32 FutureValue: 0E-2112
Month: 33 FutureValue: 0E-2178
Month: 34 FutureValue: 0E-2244
Month: 35 FutureValue: 0E-2310
Month: 36 FutureValue: 0E-2376
FORMATTED RESULTS
Monthly investment: $1.00
Yearly interest rate: 0.0%
Number of years: 3
Future value: 0E-2376
Continue? (y/n):
So clearly this still isn't working. Can anybody help?