C# - Math Formula Help - Homework
I am currently trying to calculate a monthly payment plan in C# given a loan amount, a rate, and a duration in years.
My professor provided a executable version of the assignment, but with no code. Along with that he has given us the formula to calculate monthly payments:
To get the APR, the user entered the Percentage in a text box and I divided the number by 100 to get it in a decimal.
To get the Duration, the user entered the Years in a text box and I the multiplied the number by 12 to get it in months.
My C# code lo开发者_StackOverflow中文版oks like this:
payment = (loanAmount*aprPercent) / (1-Math.Pow((1+aprPercent), -durationMonths));
I have triple checked to see that that code follows the formula my professor provided.
Yet when I use the exact same input in my professors executable as I do in my application the numbers are way off.
For Example: I enter 12% APR, 12 Years Duration, and 12 Dollar LoanAmount on both mine and my professors application. His comes to a $0.16 monthly payment and mine comes to a $1.44 monthly payment.
Remember that I did devide 12%/100 to get a .12 for aprPercent. And multiplied 12*12 to get durationMonths. So I do not see why our outputs are way different.
Please note that this is not the extent of my assignment, this is just a part of the assignment that I need to figure out before I can finish the rest of the assignment. It is due friday of next week, so I did not procrastinate. I am not trying to use the people of StackOverflow to solve my homework for me, I am using them as a resource to help me solve my issue and will state this thread in the comments of my application.
Double-check your time units.
Sounds like you may be using an annual interest rate, yet a term that is measured in months.
You should be using the monthly percentage figure for the rate, not the yearly one. This is fairly evident since, at $1.44 a month, you'd pay off your loan in about nine months rather than twelve years :-)
12% p.a. gives you a yearly aprPercent
of 0.12
but the monthly equivalent is 0.12 / 12 = 0.01
:
12 x 0.01
------------
-144
1 - 1.01
0.12
= ---------------
1 - 0.238628425
0.12
= -----------
0.761371575
= 0.157610297
Or sixteen cents, as your educator has told you.
精彩评论