-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
148 lines (131 loc) · 3.47 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="user-scalable=no">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>房贷(等额本息)计算器</title>
</head>
<body>
<table>
<tr>
<td>贷款总金额</td>
<td>
<input id="loan" placeholder="输入总房款金额" value="1000" onchange="cal();"/>万
</td>
</tr>
<tr>
<td>贷款时长</td>
<td>
<input id="years" placeholder="贷款年数" value="25" onchange="cal();"/>年 +
<input id="months" placeholder="贷款月数" value="0" onchange="cal();"/>月
</td>
</tr>
<tr>
<td>贷款利率</td>
<td>
<input id="rate" placeholder="贷款利率" value="4.75" onchange="cal();"/>%
</td>
</tr>
<tr>
<td>月供</td>
<td>
<span id="loanPerMonth"></span>元
</td>
</tr>
<tr>
<td>总利息</td>
<td>
<span id="totalInterest"></span>元
</td>
</tr>
</table>
<h2 style="text-align:center">贷款月份明细</h2>
<table id="monthDetail"></table>
</body>
<script>
function cal(){
// 贷款金额
var loan = parseFloat(document.getElementById("loan").value) * 10000
// 贷款年限
var years = parseInt(document.getElementById("years").value) || 0
// 贷款月份
var months = parseInt(document.getElementById("months").value) || 0
// 贷款利率
var rate = parseFloat(document.getElementById("rate").value / 100) || 0.0475
var totalMonths = years * 12 + months
if (!loan || !totalMonths) {
document.getElementById("monthDetail").innerHTML = ''
return
}
// 月供
var loanPerMonth = loan * (rate / 12 * Math.pow((1 + rate / 12), totalMonths)) / (Math.pow((1 + rate / 12), totalMonths) - 1)
document.getElementById('loanPerMonth').innerHTML = loanPerMonth ? loanPerMonth.toFixed(2) : '?'
// 总利息
var totalInterest = loanPerMonth * totalMonths - loan
document.getElementById('totalInterest').innerHTML = totalInterest ? totalInterest.toFixed(2) : '?'
// 贷款还款明细
var monthA = []
var monthB = []
var currentInterest = 0
var totalbj = 0
var rows = '<tr><th>月数</th><th>本金</th><th>利息</th><th>已还本金</th><th>已付利息</th><th>剩余本金</th></tr>'
for(var i = 0;i < totalMonths; i++){
monthA[i] = loan * rate / 12
currentInterest = currentInterest + monthA[i]
monthB[i] = loanPerMonth - monthA[i]
totalbj = totalbj + monthB[i]
loan = loan - monthB[i]
if (i%12 === 0){
rows += '<tr class="yearStart">'
} else {
rows += '<tr>'
}
rows += '<td>第'+(parseInt(i / 12) + 1)+'年第'+( i % 12 + 1)+'月</td><td>'+monthB[i].toFixed(2)+'</td><td>'+monthA[i].toFixed(2)+'</td><td>'+totalbj.toFixed(2)+'</td><td>'+currentInterest.toFixed(2)+'</td><td>'+loan.toFixed(2)+'</td></tr>'
}
document.getElementById("monthDetail").innerHTML = rows
}
cal();
</script>
</html>
<style>
body {
font-size: 24px;
line-height: 60px;
margin: 16px;
}
.yearStart {
border-top: 4px solid black;
}
table {
width: 100%;
}
table, td, th {
border-collapse:collapse;
border: 1px solid grey;
padding: 10px;
text-align: right;
}
th {
position: sticky;
top: 0;
background: white;
text-align: center;
}
td:first-child {
text-align: center;
}
input {
border: 1px solid grey;
text-align: center;
width: 150px;
height: 60px;
font-size: 30px;
margin: 0 20px;
}
#loan {
width: 300px;
}
#rate {
width: 200px;
}
</style>