JS信用卡真实利率计算器

算法

第一个月,还款前我手中持有现金10000元,本月我应付的利息是10000×0.66%=66元。

第二个月,因为我还了833.33元本金,还款前我手中持有现金10000-833.33=9166.67元,本月我应付的利息是9166.67×0.66%=60.5元

第三个月,我又还了833.33元本金,还款前我手中持有现金9166.67-833.33=8333.34元,本月我应付的利息是8333.34×0.66%=55元

依次类推……

第十二个月,还款前我手中仅持有833.33元,本月我应付利息833.33×0.66%=5.5元

按以上算法,我本月还欠银行多少钱,就付相应的利息,已还部分就不应该付利息,这样,把以上每个月利息依次相加,算出来我12个月只应该付出429元利息。

假如我持有10000元整整12个月,应该付792元利息,但我每月都在还钱,只为没还的部分付利息,就应该付429元利息。无形中我多付了(792-429)/429*100%=84.62%。

这84.62%就是真实年利率比名义年利率多出来的部分。

那么真实年利率就是7.92%×(1+84.62%)=14.62%。

代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width">
		<title>信用卡真实利率计算器</title>
		<style>
			body,p,span,div{font-size:12px;font-family:"MS YaHei";color:#555;margin:0;padding:0}
			body{background-color:#eee}
			p{text-align:left;width:100%}
			.width400{max-width:450px;width:95%}
			.height30{height:30px;line-height:30px}
			.font0{font-size:0}
			.to-inline-block{display:inline-block;}
			.aligncenter{text-align:center}
			.margincenter{margin:0 auto}
			.label{display:block;width:100%;text-align:left;font-weight:300;font-size:14px}
			.numbers{display:block;width:95%;margin:0 auto;border:1px solid #aaa;padding:5px;border-radius:5px;color:#ccc}
			.btn{height:40px;width:100%;color:#fff;background-color:#4976d6;border:0;border-radius:5px}
			.btn:hover{background-color:#2561e2;cursor:pointer}
			.output-table{margin-top:30px}
			.output-cell{padding:5px;font-size:14px;vertical-align:bottom;margin:-1px 0 0 -1px}
			.output-cell-1{width:15%}
			.output-cell-2,.output-cell-3,.output-cell-4{width:25%}
			.output-rows {margin:25px auto}
			.output-rows span{font-size:14px;border:1px solid #aaa;padding:10px;vertical-align:bottom;font-weight:300}
			#count p{margin:10px auto}
			#real,.header-list{background-color:#ccc;font-weight:300}
			#permonth>div:nth-child(2n+1){background-color:#fff}
			#list{background-color:#fff}
			.hidden{display:none}
			.collect{padding:10px;border:1px solid #aaa;font-size:16px;font-weight:300;margin:-1px 0 0 -1px;line-height:30px}
			.collect p{text-align:right;font-size:18px}
		</style>
		<script>
			
			function hiddenValue(obj){
				var e = document.getElementById(obj);
				if( e.value != "" ) e.value = "";
			}
			
			function displayValue(obj){
				var e = document.getElementById(obj);
				if( e.value == "" ) e.value = "请不要留空!";
			}
		
			function count(){
				var ratio = parseFloat(document.getElementById( "ratio" ).value);
				var total = parseFloat(document.getElementById( "total" ).value);
				var num = parseFloat(document.getElementById( "number" ).value);
				
				console.log( "月利率="+ratio+"%\n总金额="+total+"\n期数="+num );
				
				if( isNaN(ratio) ||  isNaN(total) || isNaN(num) ) {
					window.alert("请填入有效的数字!");
					return;
				}
				
				ratio = ratio/100;
				
				//a:计算每月需还的本金,aa:计算每月利息金额
				var a = total/num
				var aa = total*ratio;
				
				//b:计算表面的年利率
				var b = ratio*12
				
				//c:计算总还款额,cc:计算总还入利息
				var cc = aa*num
				var c = total+cc

				//perMonth:计算还款过程中每月的真实利率, totalRatio:计算真实年利率
				var ratio_per_month;
				var totalRatio = 0;
				var html = "";
				
				document.getElementById( "output-table" ).classList.remove( "hidden" );
				
				for( i=0; i<num; i++ ){
					ratio_per_month = (total-i*a)*ratio;
					totalRatio += ratio_per_month;
					html += '<div class="body-list"><span class="to-inline-block height30 aligncenter output-cell output-cell-1">第'+(i+1)+'月</span><span class="to-inline-block height30 aligncenter output-cell output-cell-2">'+(total-(i*a)).toFixed(2)+'</span><span class="to-inline-block height30 aligncenter output-cell output-cell-3">'+(total-((i+1)*a)).toFixed(2)+'</span><span class="to-inline-block height30 aligncenter output-cell output-cell-4">'+ratio_per_month.toFixed(2)+'</span></div>';
					//输出每月详情
					document.getElementById( "permonth" ).innerHTML = html;
				}
				//计算真实年利率
				totalRatio = b*(1+(cc-totalRatio)/totalRatio);
					
				//输出各个数值
				document.getElementById( "list" ).innerHTML ='<div class="collect">每月需还本金:<p>'+a.toFixed(2)+'</p></div><div class="collect">每月实付利息:<p>'+aa.toFixed(2)+'</p></div><div class="collect">总还款金额:<p>'+c.toFixed(2)+'</p></div><div class="collect">总产生利息:<p>'+cc.toFixed(2)+'</p></div>';
				
				//输出表面年利率及真实年利率
				document.getElementById( "real" ).innerHTML ='<div class="collect">表面年利率:'+(b*100).toFixed(2)+'%</div><div class="collect">真实年利率:'+(totalRatio*100).toFixed(2)+'%</div>';
				
				document.getElementById( "reset" ).classList.remove( "hidden" );
			}
			
			function reset(){
				document.getElementById( "output-table" ).classList.add( "hidden" );
				document.getElementById( "reset" ).classList.add( "hidden" );
				document.getElementById( "ratio" ).value = 0.66;
				document.getElementById( "total" ).value = 10000;
				document.getElementById( "number" ).value = 12;
			}
		</script>
	</head>
	<body class="aligncenter">
		
		<div class="table">
		
			<div id="count" class="width400 margincenter">
				<p><span class="label to-inline-block height30">总金额:</span><input class="numbers height30" type="text" id="total" name="total" value="10000" onfocus="hiddenValue('total')" onblur="displayValue('total')"></p>
				<p><span class="label to-inline-block height30">月利率(%):</span><input class="numbers height30" type="text" id="ratio" name="ratio" value="0.66" onfocus="hiddenValue('ratio')" onblur="displayValue('ratio')"></p>
				<p><span class="label to-inline-block height30">期数:</span><input class="numbers height30" type="text" id="number" name="number" value="12" onfocus="hiddenValue('number')" onblur="displayValue('number')"></p>
				<p><input type="button" id="count-btn" name="count" value="计算" onClick="count()" class="btn"></p>
			</div>
			
			<div id="output-table" class="output-table width400 margincenter hidden">
				<p id="list" class="width400 output-rows"></p>
				<p class="font0 header-list">
					<span class="to-inline-block height30 aligncenter output-cell output-cell-1">期数</span>
					<span class="to-inline-block height30 aligncenter output-cell output-cell-2">还款前本金</span>
					<span class="to-inline-block height30 aligncenter output-cell output-cell-3">还款后本金</span>
					<span class="to-inline-block height30 aligncenter output-cell output-cell-4">应付利息</span>
				</p>
				<p id="permonth"></p>
				
				<p id="real"class="width400 output-rows"></p>
				<p><input type="button" id="reset" name="reset" value="重置" onClick="reset()" class="btn"></p>
			</div>
			
		</div>
		
		<p class="height30 aligncenter">CopyRight &copy; 2019 由<a href="http://www.ixcrap.com">Art-Molec</a>制作</p>
	</body>
</html>

演示

信用卡分期真实利率计算器

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值