純js版本房貸計算器

純潔的程式碼發表於2019-10-18
前言
純js版本的房貸計算器,介面精美。
介面 純js版本房貸計算器
原始碼地址:
github地址:
https://github.com/confidence68/houseCal
預覽地址:
https://www.haorooms.com/uploads/loancal/
核心程式碼
[JavaScript]
純文字檢視
複製程式碼
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//等額本息計算
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
}
}

本帖轉自:https://www.haorooms.com/post/js_housecal


相關文章