javascript
JS信用卡真实利率计算器
?
算法
第一個(gè)月,還款前我手中持有現(xiàn)金10000元,本月我應(yīng)付的利息是10000×0.66%=66元。
第二個(gè)月,因?yàn)槲疫€了833.33元本金,還款前我手中持有現(xiàn)金10000-833.33=9166.67元,本月我應(yīng)付的利息是9166.67×0.66%=60.5元
第三個(gè)月,我又還了833.33元本金,還款前我手中持有現(xiàn)金9166.67-833.33=8333.34元,本月我應(yīng)付的利息是8333.34×0.66%=55元
依次類推……
第十二個(gè)月,還款前我手中僅持有833.33元,本月我應(yīng)付利息833.33×0.66%=5.5元
按以上算法,我本月還欠銀行多少錢,就付相應(yīng)的利息,已還部分就不應(yīng)該付利息,這樣,把以上每個(gè)月利息依次相加,算出來(lái)我12個(gè)月只應(yīng)該付出429元利息。
假如我持有10000元整整12個(gè)月,應(yīng)該付792元利息,但我每月都在還錢,只為沒(méi)還的部分付利息,就應(yīng)該付429元利息。無(wú)形中我多付了(792-429)/429*100%=84.62%。
這84.62%就是真實(shí)年利率比名義年利率多出來(lái)的部分。
那么真實(shí)年利率就是7.92%×(1+84.62%)=14.62%。
代碼
<!DOCTYPE html> <html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>信用卡真實(shí)利率計(jì)算器</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 = "請(qǐng)不要留空!";}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期數(shù)="+num );if( isNaN(ratio) || isNaN(total) || isNaN(num) ) {window.alert("請(qǐng)?zhí)钊胗行У臄?shù)字!");return;}ratio = ratio/100;//a:計(jì)算每月需還的本金,aa:計(jì)算每月利息金額var a = total/numvar aa = total*ratio;//b:計(jì)算表面的年利率var b = ratio*12//c:計(jì)算總還款額,cc:計(jì)算總還入利息var cc = aa*numvar c = total+cc//perMonth:計(jì)算還款過(guò)程中每月的真實(shí)利率, totalRatio:計(jì)算真實(shí)年利率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;}//計(jì)算真實(shí)年利率totalRatio = b*(1+(cc-totalRatio)/totalRatio);//輸出各個(gè)數(shù)值document.getElementById( "list" ).innerHTML ='<div class="collect">每月需還本金:<p>'+a.toFixed(2)+'</p></div><div class="collect">每月實(shí)付利息:<p>'+aa.toFixed(2)+'</p></div><div class="collect">總還款金額:<p>'+c.toFixed(2)+'</p></div><div class="collect">總產(chǎn)生利息:<p>'+cc.toFixed(2)+'</p></div>';//輸出表面年利率及真實(shí)年利率document.getElementById( "real" ).innerHTML ='<div class="collect">表面年利率:'+(b*100).toFixed(2)+'%</div><div class="collect">真實(shí)年利率:'+(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">期數(shù):</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="計(jì)算" 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">期數(shù)</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">應(yīng)付利息</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 © 2019 由<a href="http://www.ixcrap.com">Art-Molec</a>制作</p></body> </html>演示
信用卡分期真實(shí)利率計(jì)算器
?
總結(jié)
以上是生活随笔為你收集整理的JS信用卡真实利率计算器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实战 SQL Server 2008 数
- 下一篇: Android学习笔记(八)XML文档的