Reputation: 21
I am currently trying to convert an equation in google sheets to Javascript but I am getting different results.
I am trying to replicate the following formula from here:
Original equation:
F = P*(1+rate)^nper + A*( ((1+rate)^nper - 1)/rate )
This is what I currently have: (In my actual code snippet below names of variables are different)
v = ((p*(Math.pow((1+rate),nper))) + (A*(Math.pow((1+rate),nper - 1))/rate));
Extra Information from original site:
r = nominal annual interest rate (decimal)
n = number of compounding periods per year
p = number of payment periods per year
rate = rate per payment period
nper = total number of payment periods
A = an amount added to the principal at the end of each payment period
rate = ((1+r/n)^(n/p))-1
nper = p * t
Total Payments = A*nper
Total Interest = F - P - Total Payments
Full context (My Vue Code):
var app = new Vue({
el: '#app',
data: {
principal: 0,
interest_rate: 0,
time_invested: 0,
additional_investment: 0,
additional_investment_frequency : 1,
options: {
additional_investment_frequency: {
"1": "Yearly",
"12": "Monthly",
"26": "Fortnightly",
"52": "Weekly",
"365": "Daily"
}
},
compound_frequency : 1,
options: {
compound_frequency: {
"1": "Yearly",
"12": "Monthly"
}
}
},
methods:{
},
computed: {
total_pay_periods: function () {
// calculate total number of pay periods
total_pay_periods = parseFloat(this.additional_investment_frequency) * parseFloat(this.time_invested);
return parseFloat(this.additional_investment_frequency) * parseFloat(this.time_invested);
},
total_additional_investment: function () {
// calculate total number of pay periods
total_pay_periods = parseFloat(this.additional_investment_frequency) * parseFloat(this.time_invested);
// times total pay periods by additional investment installment amount to get overall total additional investment
return parseFloat(this.additional_investment) * total_pay_periods;
},
rate: function () {
ir = parseFloat(this.interest_rate) / 100;
cf = parseFloat(this.compound_frequency);
aif = parseFloat(this.additional_investment_frequency);
return (Math.pow((1+ir/cf),(cf/aif))-1);
// return (((1+ir/cf)^(cf/aif))-1);
},
future_value: function () {
p = parseFloat(this.principal);
r = parseFloat(this.rate);
tpp = parseFloat(this.total_pay_periods);
ai = parseFloat(this.additional_investment);
// F = P*(1+rate)^nper + A*( ((1+rate)^nper - 1)/rate )
// https://www.vertex42.com/Calculators/compound-interest-calculator.html#rate-per-period
// calculate final future value
v = ((p*(Math.pow((1+r),tpp))) + (ai*(Math.pow((1+r),tpp - 1))/r));
// convert to 2 decimal places
return v.toFixed(2);
}
}
})
Upvotes: 0
Views: 448
Reputation: 801
There is a mistake in your second term (A*(Math.pow((1+rate),nper - 1))/rate));
the -1
is going to the power
function. Correct way would be (A*(Math.pow((1+rate),nper) - 1)/rate));
Upvotes: 1