首页 > 代码库 > c#实现房贷计算的方法源码

c#实现房贷计算的方法源码

        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "application/json";            string json=string.Empty;            string dktype = (!string.IsNullOrEmpty(context.Request.Form["dktype"]) ? context.Request.Form["dktype"].ToString().Trim() : "0");//贷款方式            string calculatype = (!string.IsNullOrEmpty(context.Request.Form["calculatype"]) ? context.Request.Form["calculatype"].ToString().Trim() : "0");//计算方式            string paymentType = (!string.IsNullOrEmpty(context.Request.Form["paymentType"]) ? context.Request.Form["paymentType"].ToString().Trim() : "0");//还款方式            string unitPrice = (!string.IsNullOrEmpty(context.Request.Form["unitPrice"]) ? context.Request.Form["unitPrice"].ToString().Trim() : "0");//单价            string acreage =(!string.IsNullOrEmpty(context.Request.Form["acreage"])?context.Request.Form["acreage"].ToString().Trim():"0");//面积            string mortgage = (!string.IsNullOrEmpty(context.Request.Form["mortgage"])?context.Request.Form["mortgage"].ToString().Trim():"0");//按揭成数            string loanceiling = (!string.IsNullOrEmpty(context.Request.Form["loanceiling"]) ? context.Request.Form["loanceiling"].ToString().Trim() : "0");//贷款总额            string countYears = (!string.IsNullOrEmpty(context.Request.Form["countYears"]) ? context.Request.Form["countYears"].ToString().Trim() : "0");//按揭年数            string lilv = (!string.IsNullOrEmpty(context.Request.Form["lilv"]) ? context.Request.Form["lilv"].ToString().Trim() : "0");//年利率            //组合型贷款            string syx_loanceiling = (!string.IsNullOrEmpty(context.Request.Form["syx_loanceiling"]) ? context.Request.Form["syx_loanceiling"].ToString().Trim() : "0");//商业贷款            string gjj_loanceiling = (!string.IsNullOrEmpty(context.Request.Form["gjj_loanceiling"]) ? context.Request.Form["gjj_loanceiling"].ToString().Trim() : "0");//公积金贷款            string sy_lilvValues = (!string.IsNullOrEmpty(context.Request.Form["sy_lilvValues"]) ? context.Request.Form["sy_lilvValues"].ToString().Trim() : "0");//商业贷款利率            string gjj_lilvValues = (!string.IsNullOrEmpty(context.Request.Form["gjj_lilvValues"]) ? context.Request.Form["gjj_lilvValues"].ToString().Trim() : "0");//公积金贷款利率            switch (dktype)            {                case "sydk"://商业贷款                case "gjjdk"://公积金贷款                    switch (calculatype)                    {                        case "calcula_one"://根据面积,单价计算                            json = returnResult(double.Parse(unitPrice), double.Parse(acreage), double.Parse(mortgage), Utils.ObjToInt(countYears, 1), double.Parse(lilv), paymentType);                            break;                        case "calcula_two"://根据贷款总额计算                            json = returnResult(double.Parse(loanceiling), int.Parse(countYears), double.Parse(lilv), paymentType);                            break;                    }                    break;                case "zhxdk"://组合型贷款                    json = returnResult(double.Parse(syx_loanceiling), double.Parse(sy_lilvValues), double.Parse(gjj_loanceiling), double.Parse(gjj_lilvValues), Utils.ObjToInt(countYears,1), paymentType);                    break;            }            context.Response.Write(json);        }        /// <summary>        /// 商业贷款-根据面积,单价计算        /// </summary>        /// <param name="unitPrice">单价</param>        /// <param name="acreage">面积</param>        /// <param name="mortgage">按揭成数</param>        /// <param name="countYears">按揭年数</param>        /// <param name="lilv">年利率</param>        /// <param name="paymentType">还款方式</param>        /// <returns></returns>        private string returnResult(double unitPrice, double acreage, double mortgage, int countYears, double lilv, string paymentType)        {            StringBuilder json = new StringBuilder();            double newlilv = lilv / 100/12;//月利率            double purchase=0;//房款总额;            double Loan=0;//贷款总额            double monthlypayments=0;//平均月供            double Total=0;//还款总额            double Interest=0;//总利息            double Shoufu=0;//首付            switch (paymentType.Trim())            {                case "debx"://等额本息                    purchase = Math.Round(unitPrice * acreage,2);                    Loan = Math.Round(purchase * mortgage,2);                    monthlypayments =Math.Round((Loan * newlilv * Math.Pow(1 + newlilv, countYears * 12)) / (Math.Pow(1 + newlilv, countYears * 12) - 1),2);                    Total = Math.Round(monthlypayments * countYears * 12,2);                    Interest =  Math.Round(Total - Loan,2);                    Shoufu =  Math.Round(purchase - Loan,2);                    json.Append("{\"purchase\":\"" + purchase.ToString() + "\",\"Loan\":\"" + Loan.ToString() + "\",\"Forthemonth\":\"" + monthlypayments.ToString() + "\",\"Total\":\"" + Total.ToString() + "\",\"Interest\":\"" + Interest.ToString() + "\",\"Shoufu\":\"" + Shoufu.ToString() + "\",\"months\":\"" + countYears * 12 +"(月)"+ "\"}");                    break;                case "debj"://等额本金                    purchase = Math.Round(unitPrice * acreage,2);                    Loan =Math.Round(purchase * mortgage,2);                    string monthsPay = string.Empty;                    for (int i = 0; i < countYears * 12; i++)                    {                        monthlypayments =Loan / (countYears * 12) + (Loan - Loan / (countYears * 12) * i) * newlilv;                        monthsPay +="第"+ (i + 1) + "个月," + Math.Round(monthlypayments,2) + "(元)\\r\\n";//月均金额                        Total =monthlypayments + Total;//还款总额                    }                    Interest =Math.Round(Total - Loan,2);                    Shoufu = Math.Round(purchase - Loan,2);                    json.Append("{\"purchase\":\"" + purchase.ToString() + "\",\"Loan\":\"" + Loan.ToString() + "\",\"Forthemonth\":\"" + monthsPay.ToString() + "\",\"Total\":\"" + Math.Round(Total,2) + "\",\"Interest\":\"" + Interest.ToString() + "\",\"Shoufu\":\"" + Shoufu.ToString() + "\",\"months\":\"" + countYears * 12 + "(月)" + "\"}");                    break;            }            return json.ToString();        }        /// <summary>        /// 商业贷款-根据贷款总额计算        /// </summary>        /// <param name="countYears">贷款总额</param>        /// <param name="countYears">按揭年数</param>        /// <param name="lilv">年利率</param>        /// <param name="paymentType">还款方式</param>        /// <returns></returns>        private string returnResult(double loanceiling, int countYears, double lilv, string paymentType)        {            StringBuilder json = new StringBuilder();            double newlilv = lilv / 100 / 12;//月利率            double Loan = loanceiling;//贷款总额            double monthlypayments = 0;//平均月供            double Total = 0;//还款总额            double Interest = 0;//总利息            switch (paymentType.Trim())            {                case "debx"://等额本息                    monthlypayments = Math.Round((Loan * newlilv * Math.Pow(1 + newlilv, countYears * 12)) / (Math.Pow(1 + newlilv, countYears * 12) - 1), 2);                                        Total = Math.Round(monthlypayments * countYears * 12, 2);                    Interest = Math.Round(Total - Loan, 2);                    json.Append("{\"purchase\":\"略\",\"Loan\":\"" + Loan.ToString() + "\",\"Forthemonth\":\"" + monthlypayments.ToString() + "\",\"Total\":\"" + Total.ToString() + "\",\"Interest\":\"" + Interest.ToString() + "\",\"Shoufu\":\"0\",\"months\":\"" + countYears * 12 + "(月)"+"\"}");                    break;                case "debj"://等额本金                    string monthsPay = string.Empty;                    for (int i = 0; i < countYears * 12; i++)                    {                        monthlypayments =Loan / (countYears * 12) + (Loan - Loan / (countYears * 12) * i) * newlilv;                        monthsPay +="第"+ (i + 1) + "个月," + Math.Round(monthlypayments,2) + "(元)\\r\\n";//月均金额                        Total =monthlypayments + Total;//还款总额                    }                    Interest = Math.Round(Total - Loan, 2);                    json.Append("{\"purchase\":\"略\",\"Loan\":\"" + Math.Round(Loan,2) + "\",\"Forthemonth\":\"" + monthsPay.ToString() + "\",\"Total\":\"" + Math.Round(Total, 2) + "\",\"Interest\":\"" + Interest.ToString() + "\",\"Shoufu\":\"0\",\"months\":\"" + countYears * 12 + "(月)" + "\"}");                    break;            }            return json.ToString();        }        /// <summary>        /// 组合型贷款         /// </summary>        /// <param name="syx_loanceiling">商业贷款额</param>        /// <param name="sy_lilvValues">商业贷款利率(年利率)</param>        /// <param name="gjj_loanceiling">公积金贷款额</param>        /// <param name="gjj_lilvValues">公积金贷款额利率(年利率)</param>        /// <param name="countYears">贷款期限</param>        /// <param name="paymentType">还款方式</param>        /// <returns></returns>        private string returnResult(double syx_loanceiling, double sy_lilvValues, double gjj_loanceiling,double gjj_lilvValues, int countYears,string paymentType)        {            StringBuilder json = new StringBuilder();            double sy_newlilv = sy_lilvValues / 100 / 12;//月利率            double sy_Loan = syx_loanceiling;//贷款总额            double sy_monthlypayments = 0;//平均月供            double sy_Total = 0;//商业还款总额            double sy_Interest = 0;//商业贷款利息            double gjj_newlilv = gjj_lilvValues / 100 / 12;//月利率            double gjj_Loan = gjj_loanceiling;//贷款总额            double gjj_monthlypayments = 0;//平均月供            double gjj_Total = 0;//公积金还款总额            double gjj_Interest = 0;//公积金贷款利息            switch (paymentType.Trim())            {                case "debx"://等额本息                    //商业贷款                    sy_monthlypayments = Math.Round((sy_Loan * sy_newlilv * Math.Pow(1 + sy_newlilv, countYears * 12)) / (Math.Pow(1 + sy_newlilv, countYears * 12) - 1), 2);                    sy_Total = Math.Round(sy_monthlypayments * countYears * 12, 2);                    sy_Interest = Math.Round(sy_Total - sy_Loan, 2);                    //公积金贷款                    gjj_monthlypayments = Math.Round((gjj_Loan * gjj_newlilv * Math.Pow(1 + gjj_newlilv, countYears * 12)) / (Math.Pow(1 + gjj_newlilv, countYears * 12) - 1), 2);                    gjj_Total = Math.Round(gjj_monthlypayments * countYears * 12, 2);                    gjj_Interest = Math.Round(gjj_Total - gjj_Loan, 2);                    double monthlypayments = sy_monthlypayments + gjj_monthlypayments;                    double Total = sy_Total + gjj_Total;                    double Interest = sy_Interest + gjj_Interest;                    double Loan=Math.Round( sy_Loan + gjj_Loan,2);                    json.Append("{\"purchase\":\"略\",\"Loan\":\"" + Loan.ToString() + "\",\"Forthemonth\":\"" + monthlypayments.ToString() + "\",\"Total\":\"" + Total.ToString() + "\",\"Interest\":\"" + Interest.ToString() + "\",\"Shoufu\":\"0\",\"months\":\"" + countYears * 12 + "(月)" + "\"}");                    break;                case "debj"://等额本金                    string monthsPay = string.Empty;                    double newmonthlypayments;;                    double newTotal=0;                    double newInterest;                    double newLoan;                    for (int i = 0; i < countYears * 12; i++)                    {                        sy_monthlypayments = sy_Loan / (countYears * 12) + (sy_Loan - sy_Loan / (countYears * 12) * i) * sy_newlilv;                        gjj_monthlypayments =gjj_Loan / (countYears * 12) + (gjj_Loan - gjj_Loan / (countYears * 12) * i) * gjj_newlilv;                        newmonthlypayments = sy_monthlypayments + gjj_monthlypayments;                        monthsPay +="第"+ (i + 1) + "个月," + Math.Round(newmonthlypayments) + "(元)\\r\\n";//月均金额                        sy_Total = sy_monthlypayments + sy_Total;                        gjj_Total = gjj_monthlypayments + gjj_Total;                        newTotal = gjj_Total + sy_Total;//还款总额                    }                    sy_Interest =sy_Total - sy_Loan;                    gjj_Interest =gjj_Total - gjj_Loan;                    newInterest = gjj_Interest + sy_Interest;                    newLoan = sy_Loan + gjj_Loan;                    json.Append("{\"purchase\":\"略\",\"Loan\":\"" + Math.Round(newLoan, 2) + "\",\"Forthemonth\":\"" + monthsPay.ToString() + "\",\"Total\":\"" + Math.Round(newTotal,2) + "\",\"Interest\":\"" + Math.Round(newInterest, 2) + "\",\"Shoufu\":\"0\",\"months\":\"" + countYears * 12 + "(月)" + "\"}");                    break;            }            return json.ToString();        }

 

c#实现房贷计算的方法源码