var CalculatorHkbj = { els: { //贷款总额 money: $("#DataMoney"), //贷款年限 year: $("#DataYear"), //贷款利率 lilv: $("#DataLilv"), //等额本息-首期还款 xiFirst: $("#ResultXiFirst"), //等额本息-期末还款 xiLast: $("#ResultXiLast"), //等额本息-首期还款 xiAll: $("#ResultXiAll"), //等额本金-首期还款 jinFirst: $("#ResultJinFirst"), //等额本金-期末还款 jinLast: $("#ResultJinLast"), //等额本金-首期还款 jinAll: $("#ResultJinAll"), //计算 run: $("#Run") }, init: function(){ var me = this; this.els.run.click(function(){ if(me.els.money.val() == ""){ alert("贷款总额不能为空!"); }else{ var data = { money: me.els.money.val(), year: me.els.year.val(), lilv: me.els.lilv.val() }; //计算本息方式 me.doXi(data); //计算本金方式 me.doJin(data); } }); }, //计算本息方式 doXi: function(data){ var _p = data.money * 10000, _s = data.lilv, _y = data.year, _months = _y*12,//月数 _ms = _s/100/12,//月利率 _result = {};//输出 _result.monthMoney = (_p * (_ms * Math.pow(1 + _ms, _months)) / (Math.pow((1 + _ms), _months) - 1)).toFixed(2); _result.allMoney = (_result.monthMoney * _months).toFixed(2); _result.interest = (_result.allMoney - _p).toFixed(2); this.els.xiFirst.html(this.toNum(_result.monthMoney), 1); this.els.xiLast.html(this.toNum(_result.monthMoney, 1)); this.els.xiAll.html(this.toNum(_result.allMoney, 1)); }, //计算本金方式 doJin: function(data) { var money = data.money; var percent = data.lilv/100; var date = data.year*12; var format = 1; var obj, obj1, A = money * 10000, B = percent / 12, N = date * 1; obj = { money: this.toNum(A,format), month: this.toNum(N,format), per : this.toNum(A * B * Math.pow(1+B, N)/(Math.pow((1+B),N) - 1),format), ext : this.toNum(A * N * B * Math.pow(1+B, N) / (Math.pow(1+B, N) - 1) - A,format), total: this.toNum(A * N * B * Math.pow(1+B, N)/(Math.pow(1+B, N) - 1),format) }; obj1 = { money: this.toNum(A,format), month: this.toNum(N,format), per : this.toNum(A * (1+N*B)/N,format), perA: this.toNum(A*B/N,format), ext : this.toNum((N+1)*A*B/2,format), total: this.toNum(A + (N+1)*A*B/2,format), last: this.toNum((((A/date)*percent)/12 + (A/date)),format) }; //(总额/期数)*(1+利率/12) this.els.jinFirst.html(obj1.per); this.els.jinLast.html(obj1.last); this.els.jinAll.html(obj1.total); //return {"obj": obj, "obj1": obj1}; }, toNum: function(num, format){ num = Number(num); if(!num) return ""; if (format) { return number_format(Math.round(num*1000)/1000, 0, '', ','); } else { return Math.round(num*1000)/1000; } }};CalculatorHkbj.init();function number_format (number, decimals, dec_point, thousands_sep) { number = (number + '').replace(/[^0-9+\-Ee.]/g, ''); var n = !isFinite(+number) ? 0 : +number, prec = !isFinite(+decimals) ? 0 : Math.abs(decimals), sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, dec = (typeof dec_point === 'undefined') ? '.' : dec_point, s = '', toFixedFix = function (n, prec) { var k = Math.pow(10, prec); return '' + Math.round(n * k) / k; }; // Fix for IE parseFloat(0.55).toFixed(0) = 0; s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.'); if (s[0].length > 3) { s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); } if ((s[1] || '').length < prec) { s[1] = s[1] || ''; s[1] += new Array(prec - s[1].length + 1).join('0'); } return s.join(dec);}