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();
        }
时间: 2024-10-12 22:56:47

c#实现房贷计算的方法源码的相关文章

invalidate和requestLayout方法源码分析

invalidate方法源码分析 在之前分析View的绘制流程中,最后都有调用一个叫invalidate的方法,这个方法是啥玩意?我们来看一下View类中invalidate系列方法的源码(ViewGroup没有重写这些方法),如下: /**  * Mark the area defined by dirty as needing to be drawn. dirty代表需要重新绘制的脏的区域  * If the view is visible, onDraw(Canvas) will be c

AtomicInteger的incrementAndGet方法源码

众所周知,i++分为三步: 1. 读取i的值 2. 计算i+1 3. 将计算出i+1赋给i 可以使用锁来保持操作的原子性,用volatile保持值的可见性和操作顺序性: 如果仅仅是计算操作,我们自然就想到了java.util.concurrent.atomic包下的原子类,则不必考虑锁的升级.获取.释放等消耗,也不必考虑锁的粒度.种类.可重入性等: 下面来看下AtomicInteger的incrementAndGet方法源码: public final int incrementAndGet()

python实现whois查询功能的方法源码

恐怕很多朋友跟我一样,使用python语言居然能实现whois服务器查询功能.下面我把代码和说明搬来给大家看看,有谁需要可以参考下.本来想直接从whois服务器查询的,但是发现要写socket 用43端口链接服务器,但是有些服务器的地址不清楚,而且查询命令貌似有改变所以不想折腾了,就想着直接用chinaz的页面实现一下算了.如下代码是在 win7下操作的,安装python3.2测试通过. Python3.2实现whois查询功能的方法源码: # -*- coding:utf-8 -*- impo

Android编程之Fragment动画加载方法源码详解

上次谈到了Fragment动画加载的异常问题,今天再聊聊它的动画加载loadAnimation的实现源代码: Animation loadAnimation(Fragment fragment, int transit, boolean enter, int transitionStyle) { 接下来具体看一下里面的源码部分,我将一部分一部分的讲解,首先是: Animation animObj = fragment.onCreateAnimation(transit, enter, fragm

Java split方法源码分析

Java split方法源码分析 1 public String[] split(CharSequence input [, int limit]) { 2 int index = 0; // 指针 3 boolean matchLimited = limit > 0; // 是否限制匹配个数 4 ArrayList<String> matchList = new ArrayList<String>(); // 匹配结果队列 5 Matcher m = matcher(inp

jQuery的getText()方法源码

/** * Utility function for retrieving the text value of an array of DOM nodes * @param {Array|Element} elem */ getText = Sizzle.getText = function( elem ) { var node, ret = "", i = 0, nodeType = elem.nodeType; if ( !nodeType ) { // If no nodeTyp

TreeSet集合的add()方法源码解析(01.Integer自然排序)

>TreeSet集合使用实例 >TreeSet集合的红黑树 存储与取出(图) >TreeSet的add()方法源码     TreeSet集合使用实例 package cn.itcast_05; import java.util.TreeSet; /* * TreeSet:能够对元素按照某种规则进行排序. * 排序有两种方式 * A:自然排序 * B:比较器排序 * * TreeSet集合的特点:排序和唯一 * * 通过观察TreeSet的add()方法,我们知道最终要看TreeMap的

.NET软件防破解方法源码混淆

其实我们只要在软件设计的关键几个环节,利用专业的控件进行保护,就可以保证软件的安全.我们可以在软件设计时和运行时对软件代码进行保护.在设计时的保护,主要的保护手段是,混淆源码:在运行时的保护主要的手段是加壳程序和授权控制.下面我们分别对混淆.加壳.授权控制的方法和采用的控件做一一讲解. 源码混淆 源码混淆就是通过对程序源码的分析,改变源码的原始面貌,降低源码可读性,可对函数甚至流程进行混淆.虽然目前很多开发工具都能进行简单的混淆,不过实用性不大,采用专业的混淆控件对程序源码能起到有效的保护,有些

jQuery方法源码解析--jQuery($)方法(一)

jQuery方法源码解析--jQuery($)方法 注: 1.本文分析的代码为jQuery.1.11.1版本,在官网上下载未压缩版即可 2.转载请注明出处 jQuery方法: 这个方法大家都不陌生,在使用过程中,它还有另外一个名字,美元符号:$,$(...)其实就是jQuery(...); 它有很多种用法,通常都返回一个jquery对象,也可以作为$(document).ready(...);的简写形式,分析之前先看一下jQuery都有什么用法. 1.jQuery( selector [, co