效果图
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>贷款计算器,利率计算器,利息计算器</title>
<style type="text/css">
body {
font-size: 14px;
line-height: 30px;
}
.t {
margin-top: 2px;
margin-bottom: 10px;
}
.l {
display: inline-block;
width: 100px;
text-align: right;
}
.r {
display: inline-block;
width: 200px;
text-align: left;
}
.d {
width: 320px;
border: 1px solid #ccc;
padding: 10px;
text-align: center;
}
.c {
width: 200px;
}
.w80 {
width: 80px;
}
.txt-r {
text-align: right;
}
.result {}
.red {
color: red;
}
.footer {
font-size: 12px;
}
</style>
</head>
<body>
<div class="d">
<h1 class="t">贷款计算器</h1>
<span class="l">贷款金额:</span>
<span class="r">
<input id="total" type="number" class="w80 txt-r" value="10000" />元
</span>
<span class="l">贷款期限:</span>
<span class="r">
<select id="num">
<script>
for (var i = 1; i <= 360; i++) {
document.write("<option>" + i + "</option>");
}
</script>
</select>个月
</span>
<span class="l">
<select id="rateType">
<option value="year">年利率</option>
<option value="month">月利率</option>
<option value="day">日利率</option>
</select>:
</span>
<span class="r">
<input id="rate" type="number" class="w80 txt-r" value="4.9" />%
</span>
<div id="rateDiv"></div>
<span class="l">还款方式:</span>
<span class="r">
<select id="repaymentMethod">
<option value="debx">等额本息</option>
<option value="debj">等额本金</option>
</select>
</span>
<input type="button" value="立即计算" class="c" onclick="count()">
<div class="result">
<span class="l">总利息:</span>
<span class="r">
<span id="totalInterest" class="red"></span>元
</span>
<span class="l">本息合计:</span>
<span class="r">
<span id="principalInterest" class="red"></span>元
</span>
<span class="l" style="vertical-align: top;">每月还款:</span>
<span class="r" id="repayment">
</span>
</div>
<div class="footer">Powered by <a href="">shanshan</a></div>
</div>
<script type="text/javascript">
function keepDecimalPlaces(value, num) {
if (isNaN(value)) {
return 0;
}
num = isNaN(num) ? 0 : num;
var n = Math.pow(10, num);
return Math.round(value * n) / (n * 1.0);
}
function count() {
var total = new Number(document.getElementById("total").value);
var num = new Number(document.getElementById("num").value);
var rateType = document.getElementById("rateType").value;
var rate = new Number(document.getElementById("rate").value);
var repaymentMethod = document.getElementById("repaymentMethod").value;
var year = 0, month = 0, day = 0;
if (rateType == "year") {
year = rate;
month = year / 12;
day = month / 30;
} else if (rateType == "month") {
month = rate;
year = month * 12;
day = month / 30;
} else {
day = rate;
month = day * 30;
year = month * 12;
}
var html = "年利率" + keepDecimalPlaces(year, 4) + "%;月利率" + keepDecimalPlaces(month, 4) + "%;日利率" + keepDecimalPlaces(day, 4) + "%";
document.getElementById("rateDiv").innerHTML = html;
if (repaymentMethod == "debx") {
debx(total, num, month / 100.0);
} else {
debj(total, num, month / 100.0);
}
}
function debx(total, num, rate) {
var temp = Math.pow(1 + rate, num);
var m = total * rate * temp / (temp - 1);
document.getElementById("totalInterest").innerHTML = keepDecimalPlaces(m * num - total, 2);
document.getElementById("principalInterest").innerHTML = keepDecimalPlaces(m * num, 2);
var html = '<span class="red">' + keepDecimalPlaces(m, 2) + '</span>元';
document.getElementById("repayment").innerHTML = html;
}
function debj(total, num, rate) {
var mbj = total / num;
document.getElementById("totalInterest").innerHTML = keepDecimalPlaces((num + 1) * total * rate / 2, 2);
document.getElementById("principalInterest").innerHTML = keepDecimalPlaces((num + 1) * total * rate / 2 + total, 2);
var html = '';
for (var i = 1; i <= num; i++) {
var m = mbj + (total - mbj * (i - 1)) * rate
html += '第' + i + '月<span class="red">' + keepDecimalPlaces(m, 2) + '</span>元<br/>';
}
document.getElementById("repayment").innerHTML = html;
}
</script>
</body>
</html>