今天在网上随手扒了一个计算理财产品收益的代码,自己改了改
其实核心代码就一句,年收益 * 日收益 / 365,就出来结果了,
但是作者做了很多判断,我也懒得删了,直接贴上来吧
顺便吐槽原作者一句,他可能不会用 toFixed()
toFixed 在做这种保留几位小数的计算时非常有用
而原作者居然先把数字 toString() 然后又加了判断,弄的非常麻烦
显然是JS基础不行,哈哈,吐槽完毕,贴代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
* {
margin: 0;
padding: 0;
}
.jsq_button {
margin: 25px auto 0;
overflow: hidden;
}
.jsq_bg td {
height: 25px;
}
.main_td_position {
color: #545454;
font-size: 14px;
padding-right: 8px;
text-align: right;
}
.inputstyle {
border: 1px solid #dadada;
font-size: 14px;
height: 25px;
margin: 6px 0;
width: 173px;
padding-left: 3px;
}
.jsbtn{
width: 80px;
height: 34px;
}
.shouyi{
padding-top: 20px;
}
</style>
<title>计算收益</title>
</head>
<body>
<div id="bt">
<table border="0" cellpadding="0" cellspacing="0" class="jsq_bg">
<tbody>
<tr>
<td class="main_td_position">购买金额:</td>
<td>
<input value="10000" class="inputstyle" id="numbsId" maxlength="9" name="numbsId" type="text"> 元
</td>
</tr>
<tr>
<td class="main_td_position">购买期限:</td>
<td>
<input value="30" class="inputstyle" id="daysId" maxlength="7" name="daysId" type="text"> 天
</td>
</tr>
<tr>
<td class="main_td_position">收益率:</td>
<td>
<input value="4" class="inputstyle" id="yqnhsyl" type="text"> %
</td>
</tr>
</tbody>
</table>
<div class="jsq_button">
<input class="jsbtn" type="button" value="开始计算" onclick="profitFun()">
</div>
<div class="shouyi">
<span>收益预计为:</span><span id="profitId"></span>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.js"></script>
<script>
function profitFun() {
var profit = $("#yqnhsyl").val(); // 收益率
var numbs = $('#numbsId').val(); // 购买金额
var daysInput = $('#daysId').val(); // 购买期限
if (numbs == '' || numbs == null || typeof (numbs) == 'undefined') {
alert('购买金额不能为空');
return;
} else {
var mathReg = /^(\+)?(0|[1-9]\d*)(\.[0-9]{1,4})?$/;
if (!mathReg.test(numbs)) {
alert('购买金额应为数字并且最多保留4位小数');
return;
} else {
if (numbs > 100000000) {
alert('请输入购买金额不大于100000000的数字');
return;
}
}
}
if (daysInput == '' || daysInput == null || typeof (daysInput) == 'undefined') {
alert('理财天数不能为空');
return;
} else {
var dayReg = /^\d+$/;
if (!dayReg.test(daysInput)) {
alert('理财天数为正整数');
return;
}
}
if (profit == '' || profit == null || typeof (profit) == 'undefined') {
alert('收益率不能为空');
return;
} else {
if (profit < 0) {
alert('收益率为正数');
return;
}
var sylReg = /^(([0-9]{1,2}))(\.[0-9]{1,2})?$/;
if (!sylReg.test(profit)) {
alert('收益率为数字,且小数点前后最多各2位');
return;
}
}
var profitYear = numbs * profit / 100; //年收益
var profitDay = profitYear / 365; //日收益
var profitDays = (profitYear * daysInput / 365).toString(); //产品周期收益
var strProfitDays;
if (profitDays.indexOf('.') > -1 && profitDays.length > (profitDays.indexOf('.') + 3)) {
// 如果这个数有小数点,而且小数点后面的数的length大于5个
// 大概意思就是小数点后面的数太多了,就舍弃
strProfitDays = profitDays.substring(0, profitDays.indexOf('.') + 3);
} else {
strProfitDays = profitDays;
}
$('#profitId').text(strProfitDays);
}
</script>
</body>
</html>