using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IndividualAssignmentLoan {
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
jmToday.Caption = Format(Now, "DDDD, D/MMMM/YYYY")
}
protected void btnCalculate_Click(object sender, EventArgs e)
{
double amount = double.Parse(jmLoanAmount.Text);
double downPay = double.Parse(jmDownPayment.Text);
double interest = double.Parse(jmInterestRate.Text);
int period = int.Parse(jmLoanPeriod.Text);
Double monthlyPay;
Double loanCost;
monthlyPay = ((amount * interest) / 1) - (Math.Pow(1/(1 + interest), period));
loanCost = monthlyPay * (period * 12);
jmMonthlyPayment.Text = monthlyPay.ToString();
jmTotalLoanCost.Text = loanCost.ToString();
}
}
}
嗨,我已写到我不明白我的贷款计算器出错的地方。我已输入我的公式以获得贷款金额......
monthlyPay = ((amount * interest) / 1) - (Math.Pow(1/(1 + interest), period));
我已经尝试过其他方法来获得正确的金额,但似乎没有什么能给我每月需要的金额。有人可以帮我配方。哦,我的时钟,我有顶级大声笑......
jmToday.Caption = Format(Now, "DDDD, D/MMMM/YYYY")
答案 0 :(得分:2)
当教师教导学生使用双倍与金钱相关的操作时,我讨厌它。总是使用小数,小孩! 128位非浮点优点(https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/decimal)
无论如何,我认为我破解了你的老师对公式的意图(你的评论至少可以说是模棱两可的)。我想你想要:
double actualAmountToPay = amount - downPay;
double monthlyPay = actualAmountToPay * interest / (1 - 1 / Math.Pow((1 + interest), period));
double totalCost = monthlyPay * (period * 12);
答案 1 :(得分:0)
保留btnClick处理解析并将每月付款计算移至单独的方法:
double CalculateMonthlyPayment(double amount, double downPayment, int termInYears, double interestRate)
{
int paymentsCount = MonthsPerYear * termInYears;
double principal = amount - downPayment;
if (interestRate == 0)
{
return principal / paymentsCount;
}
double monthlyRate = interestRate / MonthsPerYear;
double coefficient = Math.Pow(1 + monthlyRate, paymentsCount);
double monthlyPayment = principal * (monthlyRate * coefficient) / (coefficient - 1);
return monthlyPayment;
}
MonthsPerYear
是一个具有明显价值的常量。
TotalPayment
将为paymentsCount * MonthlyPayment
请注意,您需要处理例外情况:amount <= 0
,downPayment < 0
,amount <= downPayment
,termInMonths < 1
。同时决定是否允许负利率。