console
function run(flag) {
let money = parseFloat(document.getElementById("money").value);
let year = parseFloat(document.getElementById("year").value);
let win = parseFloat(document.getElementById("win").value);
let total = 0.00;
let corpus = money * year;
if(flag==1){
total = money;
corpus=money;
}
for (let i = 1; i <= year; i++) {
if(flag==1){
total = total*(1 + win / 100);
}else{
total = (total*(1 + win / 100) + money) * (1 + win / 100);
}
}
let yield0 = parseFloat(total - corpus);
let total1 = (total / 10000).toFixed(3);
let corpu1 = (corpus / 10000).toFixed(3);
let yield1 = (yield0 / 10000).toFixed(3);
document.getElementById("total").innerText = fmoney(total.toFixed(3));
document.getElementById("corpus").innerText = fmoney(corpus.toFixed(3));
document.getElementById("yield").innerText = fmoney(yield0.toFixed(3));
document.getElementById("yield-rate").innerText = (total / corpus * 100).toFixed(3);
document.getElementById("total-s").innerText = total1 + "万元";
document.getElementById("corpus-s").innerText = corpu1 + "万元";
document.getElementById("yield-s").innerText = yield1 + "万元";
}
function fmoney(s, n) {
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
t = "";
for (i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
return t.split("").reverse().join("") + "." + r;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body onload="run()">
<div class="container">
<div class="col-md-6">
<div class="container-fluid">
<p></p>
<div class="form-group input-group">
<span class="input-group-addon">本金</span>
<input type="text" class="form-control" id='money' value="10000">
<span class="input-group-addon">元/年(次)</span>
</div>
<div class="form-group input-group">
<span class="input-group-addon">交易</span>
<input type="text" class="form-control" id='year' value="20">
<span class="input-group-addon">年(次)</span>
</div>
<div class="form-group input-group">
<span class="input-group-addon">年(次)收益率</span>
<input type="text" class="form-control" id='win' value="5.0">
<span class="input-group-addon">%</span>
</div>
<div class="form-group"><input type="button" onclick="run(1)" class="btn btn-primary" value="一次本金复利"/> <small>一次投入本金,复利后续不追加本金</small></div>
<div class="form-group"><input type="button" onclick="run(0)" class="btn btn-primary" value="等额定投复利"/> <small>等额本金,多次投入同时复利</small></div>
<h3><strong>预估:<p><label id="total"></label>元≈<small id="total-s"></small></p></strong></h3>
<hr>
<div>本金:<label id="corpus">0</label>元≈<small id="corpus-s"></small></div>
<div>收益金:<label id="yield">0</label>元≈<small id="yield-s"></small></div>
<div>收益率:<label id="yield-rate">0</label>%</div>
</div>
</div>
</div>
</body>
</html>
small{color: red;}