Reputation: 776
I'm attempting to build a small program to generate a string output which will tell me the total repayable amount for a loan (compound interest on a monthly basis). I'm using two online calculators to validate my workings, however both differ to one another.
The above is validated via the following online calculator: https://www.moneysupermarket.com/loans/calculator/
However, on pretty much every other online calculator I get:
Examples:
https://en-gb.calculatestuff.com/financial/loan-amortization-calculator
Furthermore in my own code I get 1,111.53. So I'm a bit confused as to which end value is correct. I'd like to get closer to the first amount of 1,108.04 as thats the value I've been asked to find, but not sure what I'm missing here.
public void CompoundCalculator(double lenderRate, double loanPeriodInMonths, double desiredLoanAmount){
var repaymentAmount = PMT(7, 36, 1000);
var balance = desiredLoanAmount;
var totalInterest = 0.00;
for (var i = 0; i < loanPeriodInMonths;i++)
{
var monthlyInterest = Math.Round(balance * ( (lenderRate / 1200) ),2);
balance -= Math.Round(repaymentAmount - monthlyInterest,2) ;
totalInterest += monthlyInterest;
}
//do something here later with totalInterest variable
}
public static double PMT(double yearlyInterestRate, int totalNumberOfMonths, double loanAmount)
{
var rate = (double)yearlyInterestRate / 100 / 12;
var denominator = Math.Pow((1 + rate), totalNumberOfMonths) - 1;
return (rate + (rate / denominator)) * loanAmount;
}
Upvotes: 1
Views: 1955
Reputation: 785
Based on my borrowing experience (in the US only), the larger number is more commonly charged by lenders. They calculate a 'periodic rate' which could be a flat monthly rate or be based on the number of days in the lending period. Then they calculate interest on the outstanding principal at the end of the period.
If you use an online calculator from a lending institution, the values they give you will be what they will charge you for the loan. You may need to adjust your code to get a desired result, but if you borrow money from a lending institution, you will probably have a higher payment.
One of my mortgage lenders used a hybrid monthly/daily rate. When I made standard payments, the full months interest was charged regardless of when the payment was made. If I made extra payments, the principal was immediately reduced, so my next month's interest may be slightly more or less depending on which day I made my payment. Made it hard to match their calcs. All of my other lenders have used rate/12 and applied extra payments by reducing the principal for the following month.
Upvotes: 1
Reputation: 3805
Your problem is that you use the wrong rate : you make as if the monthly rate was yearlyrate/12 while it is in fact (1+yearlyrate)^(1/12)-1 (which is the rate that gives yearlyrate when you compound it 12 times.
For example, for a 7% rate, the monthly rate is ~0.5654% and not 0.5833% like your computation finds.
replace your formula from (double)yearlyInterestRate / 100 / 12 to
Math.Pow(1+(yearlyInterestRate / 100),1.0/12)-1
should give you the correct result (~30.7789$ per month)
Upvotes: 2