user5928703 user5928703 - 1 year ago 76
Java Question

I know I'm doing something wrong here with compound interest formula

Background: doing a budget portfolio program and I am trying to add in a compound interest calculator for a client's savings. Running into some problems here.
So here is the formula I currently have.

double comprinc= 25*(Math.pow((1+.05/12),(12*year)));
double futurev = saving1*(Math.pow((1+.05/12),((12*year)-1))/(.05/12));

This is the following formula broken down into two halves

Total = [ P(1+r/n)^nt ] + [ PMT * (((1 + r/n)^nt - 1) / (r/n)) ]

Total = comprinc + futurev

P= principle =25

r= rate=.05

n= number of time interest is compounded

t= the number of years =5( have year currently set to 5)

PMT= initial savings=saving1=25

The problem is that I am testing this against an official compound interest calculator
and the answers I'm getting are no where close.

For example the answer that my program reads out is $7700.28 after 5 years should be $1739.32

Answer Source

I think the key is the -1 is not part of the exponent. I am also trying to simplify here, by reducing calculations and parentheses.

double intPerPeriod = .05/12;
double numPeriods = 12*year;    
double comprinc= 25*(Math.pow(1+intPerPeriod,numPeriods)); 
double futurev = saving1*(Math.pow(1+intPerPeriod,numPeriods)-1)/intPerPeriod;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download