static double CalculateMontlyRepayment(double loanAmount, double annualInterestRate, int loanTermYears)
{
//convert yearly interest into monthly interest.
double monthlyInterestRate = annualInterestRate / 12.0;
//Figure out number of monthly payments.
int loanTermMonths = loanTermYears * 12;
//The formula to get the monthly payment amount.
double monthlyPayment = loanAmount * (monthlyInterestRate * Math.Pow(1 + monthlyInterestRate, loanTermMonths)) /
(Math.Pow(1 + monthlyInterestRate, loanTermMonths) - 1);
return monthlyPayment;
}
我可以在一张纸上完成它,但将其翻译成代码是我正在努力解决的问题。甚至尝试使用人工智能,但这只会让情况变得更糟。
利息应该是百分比。您的公式将其表示为十进制值。
将下面的行更改为:
// 将年利率百分比转换为小数,然后转换为月利率。 双倍月利率 = (年利率 / 100) / 12.0;