JavaScript实现贷款利息计算器JavaScript实现贷款利息计算器
为了简洁明了,就省去样式了,只留计算和赋值显⽰的部分~
需要⾃⾏引⼊jQuery
代码
<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""/TR/html4/loose.dtd"> <html>
<head>
<title>理财计算器</title>
<meta name="keywords" content="理财计算器">
<script type="text/javascript" src="../jquery2.1.min.js"></script>
</head>
<body>
<!--计算器开始-->
<div>
<div>
<div class="jsq_top">
<div>出借利息计算器</div>
<div>
<div>
<aa>出借⾦额</aa><input type="text" id="total"><span></span><font></font>
</div>
<div>
<aa>出借利率</aa><input type="text" id="yearRate" errormsg="" placeholder="%">
</div>
<div>
<aa>借款期限</aa>
<select name="select" id="sumTerm">
<%
for(int i=1;i<=36;i++){
%>
<option value="<%=i %>"><%=i %>个⽉</option>
<%}%>
</select>
</div>
<div>
<aa>还款⽅式</aa>
<select name="select" id="hkfs">
<option value="MYFX">先息后本</option>
<option value="DEBX">等额本息</option>
<option value="DEBJ">等额本⾦</option>
<option value="YCFQ">⼀次付清</option>
</select>
</div>
<div>
<button class="jsqks">开始计算</button>
</div>
</div>
<div id="showjs1" >
<div><h4>收益描述</h4></div>
<div class="jsq_xq">
<div>
<span>出借⾦额</span><font id="mtoal"></font>
</div>
<div>
<span>应收利息</span><font id="ghbx"></font>
</div>
<div>
<div>
<span>收回全部本息时间</span><font id="mthns"></font>
</div>
<div class="clr"></div>
</div>
</div>
<div id="showjs2" >
<div><h4>本息回收时间表</h4></div>
<div>
<table border="0" cellpadding="0" cellspacing="0"class="jstr">
</table>
</div>
</div>
</div>
</div>
</div>
<!--计算器结束-->
<script type="text/javascript">
Fixed=function(d)
{
var s=this+"";if(!d)d=0;
if(s.indexOf(".")==-1)s+=".";s+=new Array(d+1).join("0");
if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+(d+1)+"})?)\\d*$").test(s))
{
var s="0"+ RegExp.$2, pm=RegExp.$1, a=RegExp.$3.length, b=true;
if(a==d+2){a=s.match(/\d/g);if(parseInt(a[a.length-1])>4)
{
for(var i=a.length-2; i>=0; i--){a[i]=parseInt(a[i])+1;
if(a[i]==10){a[i]=0; b=i!=1;}else break;}
}
s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2");
}if(b)s=s.substr(1);return(pm+s).replace(/\.$/,"");}return this+"";
};
//不失精度计算加、减、乘、除
function accAdd(arg1,arg2){
var r1,r2,m;
try{String().split(".")[1].length;}catch(e){r1=0;}
try{String().split(".")[1].length;}catch(e){r2=0;}
m=Math.pow(10,Math.max(r1,r2));
return(arg1*m+arg2*m)/m;
}
function Subtr(arg1,arg2){
var r1,r2,m,n;
try{String().split(".")[1].length;}catch(e){r1=0;}
try{String().split(".")[1].length;}catch(e){r2=0;}
m=Math.pow(10,Math.max(r1,r2));
//last modify by deeka
//动态控制精度长度
n=(r1>=r2)?r1:r2;
return((arg1*m-arg2*m)/m).toFixed(n);
}
function accMul(arg1,arg2)
{
var m=0,String(),String();
try{m+=s1.split(".")[1].length;}catch(e){}
try{m+=s2.split(".")[1].length;}catch(e){}
return place(".",""))*place(".",""))/Math.pow(10,m); }
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
var t1=0,t2=0,r1,r2;
try{String().split(".")[1].length;}catch(e){}
try{String().split(".")[1].length;}catch(e){}
with(Math){
r1=String().replace(".",""));
r2=String().replace(".",""));
return(r1/r2)*pow(10,t2-t1);
}
}
Number.prototype.add=function(arg){
return accAdd(arg,this);
};
Number.prototype.substr=function(arg){
return Subtr(this, arg);
};
Number.prototype.mul=function(arg){
return accMul(arg,this);
};
Number.prototype.div=function(arg){
return accDiv(this, arg);
};
$("#total").on('keyup',function(event){
var $amountInput =$(this);
//响应⿏标事件,允许左右⽅向键移动
event = window.event || event;
if(event.keyCode ==37| event.keyCode ==39){
return;
}
/
/先把⾮数字的都替换掉,除了数字和.
$amountInput.val($amountInput.val().replace(/[^\d.]/g,"").
//只允许⼀个⼩数点
replace(/^\./g,"").replace(/\.{2,}/g,".").
//只能输⼊⼩数点后两位
replace(".","$#$").replace(/\./g,"").replace("$#$",".").replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'));
});
$("#total").on('blur',function(){
var $amountInput =$(this);
//最后⼀位是⼩数点的话,移除
$amountInput.val(($amountInput.val().replace(/\.$/g,"")));
});
$(".jsqks").click(function(){
//借款⾦额
var toal =$("#total").val();
//借款期限
var month =$("#sumTerm").val();
//借款年利率
var year =$("#yearRate").val();
//还款⽅式
var type =$("#hkfs").val();
//⽉利率
var monthRate =parseFloat(year)/12;
if(toal <=0){
alert("请输⼊投资⾦额");
return false;
}
if(month <=0){
alert("请输⼊投资期限");
alert("请输⼊投资期限");
return false;
}
if(year <=0){
alert("请输⼊年利率");
return false;
}
var myreg1 =/^[0-9]([0-9])*$/;//只能输⼊整数
var myreg2 =/^(([1-9]+)|([0-9]+\.[0-9]{1}))$/;//只能输⼊⼩数点后有⼀位的数字
var myreg3 =/^(([1-9]+)|([0-9]+\.[0-9]{1,2}))$/;//只能输⼊⼩数点后有两位的数字
var yeareg=/^\d+(\d|(\.[1-9]{1}))$/;
if(!st(toal)&&!st(toal)&&!st(toal)){
return;
}
if(!st(year)&&!st(year)){
return;
}
$("#showjs1").show();
$("#showjs2").show();
if(type =="DEBX"){
monthRate = monthRate/100;
//⽉还本息
//每⽉还款额=[总本⾦×⽉利率×(1+⽉利率)^还款⽉数]÷[(1+⽉利率)^总期数-1]
//power(1+x,n)*x*y/ (power(1+x,n)-1)
var yhbx =((Math.pow(1+monthRate, month)*monthRate*toal)/(Math.pow(1+monthRate, month)-1)).toFixed(2);
$("#mthns").html(month+'⽉');
$("#mtoal").html(toal+'元');
//应收利息
var yslxh =0;
$(".jstr").html("");
var content='<tr>';
content+='<th>期次</th>';
content+='<th>偿还本息(元)</th>';
content+='<th>偿还利息(元)</th>';
content+='<th>偿还本⾦(元)</th>';
content+='<th>剩余本⾦(元)</th>';
content+='</tr>';
/
/已还本⾦之和
var totalYhbj =0;
var dsbj =0;
var yhbj =0;
var  interest_cur =0;
javascript计算器代码var zhdhbj = toal;
for(var i=1;i<=month;i++){
//应还利息
interest_cur =parseFloat(Subtr(toal,totalYhbj)*monthRate).toFixed(2);
//应还本⾦
yhbj =parseFloat(Subtr(yhbx, interest_cur)).toFixed(2);
totalYhbj =accAdd(totalYhbj, yhbj);
//待收本⾦
dsbj  =parseFloat(Subtr(toal, totalYhbj)).toFixed(2);
if(i ==(month-1)){
zhdhbj =  dsbj;
}
if(i == month){
yhbj = zhdhbj;
//应还利息
interest_cur =parseFloat(Subtr(yhbx, yhbj)).toFixed(2);
totalYhbj =accAdd(totalYhbj, yhbj);
dsbj  =0;
dsbj  =0;
}
content+='<tr>';
content+='<td>'+i+'</td>';
content+='<td id="ysbx'+i+'">'+yhbx+'</td>';
content+='<td id="yslx'+i+'">'+interest_cur+'</td>';
content+='<td id="ysbj'+i+'">'+yhbj+'</td>';
content+='<td id="dsbj'+i+'">'+dsbj+'</td>';
content+='</tr>';
yslxh=accAdd(yslxh, interest_cur);
}
$("#ghbx").Fixed(2)+'元');
$(".jstr").append(content);
}else if(type =="DEBJ"){
//⽉利息
$("#mthns").html(month+'⽉');
$("#mtoal").html(toal+'元');
$(".jstr").html("");
var content='<tr>';
content+='<th>期次</th>';
content+='<th>偿还本息(元)</th>';
content+='<th>偿还利息(元)</th>';
content+='<th>偿还本⾦(元)</th>';
content+='<th>剩余本⾦(元)</th>';
content+='</tr>';
var new_ysbx =0;
var new_ysbj =0;
var new_yslx =0;
var new_sybj =0;
var yslxTotle =0;
var ysbjTotle =0;
//      new_ysbj = parseInt(toal).div(month);
new_ysbj = toal/month;
for(var i=1;i<=month;i++){
new_yslx =(toal - ysbjTotle)*(monthRate/100);
ysbjTotle = ysbjTotle + new_ysbj;
new_ysbx = new_yslx + new_ysbj;
new_sybj = toal - new_ysbj*i;
content+='<tr>';
content+='<td>'+i+'</td>';
content+='<td id="ysbx'+i+'">'+Fixed(2)+'</td>';      content+='<td id="yslx'+i+'">'+Fixed(2)+'</td>';      content+='<td id="ysbj'+i+'">'+Fixed(2)+'</td>';          content+='<td id="dsbj'+i+'">'+Fixed(2)+'</td>';      content+='</tr>';
yslxTotle =accAdd(yslxTotle,new_yslx);
}
$("#ghbx").Fixed(2)+'元');
$(".jstr").append(content);
}else if(type =="YCFQ"){
//还利息
var yslx =(monthRate * toal * month)/100;
$("#mthns").html(month+'⽉');
$("#mtoal").html(toal+'元');
$("#ghbx").Fixed(2)+'元');
$(".jstr").html("");
var content='<tr>';
content+='<th>期次</th>';

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。