公告:攜程招聘java、前端、測試、產品等,請發簡歷至[email protected],幫內推!

純js版本房貸計算器開源

4418次瀏覽

前言

這個代碼是蠻久之前寫的,當時還是在易居,純js版本的房貸計算器,界面精美【界面版權歸易居房友,請勿抄襲】。后來我基于此代碼轉換過一個react版本的,其實核心代碼都一樣,就是我這個純js里面的代碼。假如有需要,可以借鑒,但是不能原封不動的抄襲。

界面

enter image description here

源碼地址:

github地址:https://github.com/confidence68/houseCal

預覽地址:http://www.sqnjmr.live/uploads/loancal/

核心代碼

   //等額本息計算
    benxi: function (type, num, year, lilv) {
        //每月月供額=〔貸款本金×月利率×(1+月利率)^還款月數〕÷〔(1+月利率)^還款月數-1〕
        var mouth = parseInt(year) * 12,
            mouthlilv = parseFloat(lilv) / 12,
            dknum = parseFloat(num) * 10000;
        //每月月供
        var yuegong = (dknum * mouthlilv * Math.pow((1 + mouthlilv), mouth)) / (Math.pow((1 + mouthlilv), mouth) - 1);
        //總利息=還款月數×每月月供額-貸款本金
        var totalLixi = mouth * yuegong - dknum;
        //還款總額 總利息+貸款本金
        var totalPrice = totalLixi + dknum,
            leftFund = totalLixi + dknum;

        //循環月份
        var mouthdataArray = [],
            nowmouth = new Date().getMonth(),
            realmonth = 0;

        for (var i = 1; i <= mouth; i++) {
            realmonth = nowmouth + i;
            var yearlist = Math.floor(i / 12);

            realmonth = realmonth - 12 * yearlist;

            if (realmonth > 12) {
                realmonth = realmonth - 12
            }
            //console.log(realmonth)
            //每月應還利息=貸款本金×月利率×〔(1+月利率)^還款月數-(1+月利率)^(還款月序號-1)〕÷〔(1+月利率)^還款月數-1〕
            var yuelixi = dknum * mouthlilv * (Math.pow((1 + mouthlilv), mouth) - Math.pow((1 + mouthlilv), i - 1)) / (Math.pow((1 + mouthlilv), mouth) - 1);
            //每月應還本金=貸款本金×月利率×(1+月利率)^(還款月序號-1)÷〔(1+月利率)^還款月數-1〕
            var yuebenjin = dknum * mouthlilv * Math.pow((1 + mouthlilv), i - 1) / (Math.pow((1 + mouthlilv), mouth) - 1);
            leftFund = leftFund - (yuelixi + yuebenjin);
            if (leftFund < 0) {
                leftFund = 0
            }
            mouthdataArray[i - 1] = {
                monthName: realmonth + "月",
                yuelixi: yuelixi,
                yuebenjin: yuebenjin,
                //剩余還款
                leftFund: leftFund
            }
        }
        return {
            yuegong: yuegong,
            totalLixi: totalLixi,
            totalPrice: totalPrice,
            mouthdataArray: mouthdataArray,
            totalDknum: num,
            year: year
        };
    },
    //等額本金計算
    benjin: function (type, num, year, lilv) {
        var mouth = parseInt(year) * 12,
            mouthlilv = parseFloat(lilv) / 12,
            dknum = parseFloat(num) * 10000,
            yhbenjin = 0; //首月還款已還本金金額是0
        //每月應還本金=貸款本金÷還款月數
        var everymonthyh = dknum / mouth
        //每月月供額=(貸款本金÷還款月數)+(貸款本金-已歸還本金累計額)×月利率
        var yuegong = everymonthyh + (dknum - yhbenjin) * mouthlilv;
        //每月月供遞減額=每月應還本金×月利率=貸款本金÷還款月數×月利率
        var yuegongdijian = everymonthyh * mouthlilv;
        //總利息=〔(總貸款額÷還款月數+總貸款額×月利率)+總貸款額÷還款月數×(1+月利率)〕÷2×還款月數-總貸款額
        var totalLixi = ((everymonthyh + dknum * mouthlilv) + dknum / mouth * (1 + mouthlilv)) / 2 * mouth - dknum;
        //還款總額 總利息+貸款本金
        var totalPrice = totalLixi + dknum,
            leftFund = totalLixi + dknum;

        //循環月份
        var mouthdataArray = [],
            nowmouth = new Date().getMonth(),
            realmonth = 0;

        for (var i = 1; i <= mouth; i++) {
            realmonth = nowmouth + i;
            var yearlist = Math.floor(i / 12);

            realmonth = realmonth - 12 * yearlist;

            if (realmonth > 12) {
                realmonth = realmonth - 12
            }
            yhbenjin = everymonthyh * (i - 1);
            var yuebenjin = everymonthyh + (dknum - yhbenjin) * mouthlilv;
            //每月應還利息=剩余本金×月利率=(貸款本金-已歸還本金累計額)×月利率
            var yuelixi = (dknum - yhbenjin) * mouthlilv;
            leftFund = leftFund - yuebenjin;
            if (leftFund < 0) {
                leftFund = 0
            }
            mouthdataArray[i - 1] = {
                monthName: realmonth + "月",
                yuelixi: yuelixi,
                //每月本金
                yuebenjin: everymonthyh,
                //剩余還款
                leftFund: leftFund
            }
        }
        return {
            yuegong: yuegong,
            totalLixi: totalLixi,
            totalPrice: totalPrice,
            yuegongdijian: yuegongdijian,
            mouthdataArray: mouthdataArray,
            totalDknum: num,
            year: year
        }

    }

下載地址:

http://resource.haorooms.com/softshow-24-282-1.html

Tags: js房貸計算器

相關文章:

任选9场吧 他们说养孔雀赚钱是真的吗 中专学什么职业赚钱 剑三苍云如何赚钱 哪些软件看视频就可以赚钱 鸡肉加工厂赚钱吗 捕鱼大亨注册 不用投资就可赚钱的应用 公司给的股票期权可以赚钱吗 财神捕鱼刷分 免费真人杭州麻将 几十台苹果 赚钱 代理地板赚钱吗 0107李逵劈鱼下载 吉祥棋牌官方版 哪个游戏能打金赚钱 怎么利用微信支付宝赚钱的