简体   繁体   中英

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)) ] or

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM