javascript抵押计算器功能

时间:2013-11-02 16:44:29

标签: javascript backbone.js

我得到了这个几乎工作,但由于某种原因,剩余的摊销图表。我认为这可能是一个四舍五入的问题,但我不确定。如果有人有这方面的经验,你可以看看每月付款功能。这是我正在开发的一个小backbone.js应用程序的一部分,你可以看到它在行动http://rvb.chrismills.la

// Calculate mortgage based on selected values
calculateRBV: function() {
     var _this = this;
    // Make sure the correct downpayment is displaying
    _this.calculateDP();
    var _calcRent = $('#rvbRent').val();
    var _calcVal = $('#rvbBuy').val();
    var _calcDpPercent = $('#rvbDP').val()/100;
    var _calcDpAmt = _calcVal * _calcDpPercent;
    var _calcFinanced = _calcVal - _calcDpAmt;
    var _calcLength = $('#rvbTerm').val() * 12;
    var _calcRate = $('#rvbRate').val()/100/12;
    var _calcTaxes = $('#rvbTaxes').val();
    var _calcHpChange = $('#rvRentChangeVal').val();
    var _calcRentChange = $('#rvRentChangeVal').val();
    var _monthlyPayment = Math.floor((_calcFinanced*_calcRate)/(1-Math.pow(1+_calcRate,(-1*_calcLength)))*100)/100;
    var _totalPayments = _monthlyPayment * _calcLength;
    var _totalInterest = _totalPayments - _calcVal;
    var _totalRent = _calcRent * _calcLength;
    $('#rvbMonthly span').html(_this.formatNumber(_monthlyPayment));
    $('#rvbTotal span').html(_this.formatNumber(_totalPayments));
    $('#rvbInterest span').html(_this.formatNumber(_totalInterest));
    _this.buildAmortizationChart(_calcFinanced, _monthlyPayment, _calcLength, _calcRate);
    $('#rvbGraph').stop().slideDown(250);
    $('#amortizationChart').stop().slideDown(250);
},

// Build Amortization Chart
buildAmortizationChart: function(total, monthlyPayment, months, rate) {
    var _this = this;
    $('#amortizationChart tbody').html(''); // clear previous
    var _total = total;
    var _monthlyPayment = monthlyPayment;
    var _months = months;
    var _rate = rate;
    for(var i=1; i<=months; i++) {
        var _interest = _total * _rate;
        var _principal = _monthlyPayment - _interest;
        _total = _total - _principal;
var aRow = '';
aRow += '<tr>';
aRow += '<td width="20%">';
aRow += i;
aRow += '</td>';
aRow += '<td width="20%">';
aRow += '$' + _monthlyPayment;
aRow += '</td>';
aRow += '<td width="20%">';
aRow += '$' + _this.formatNumber(_principal);
aRow += '</td>';
aRow += '<td width="20%">';
aRow += '$' + _this.formatNumber(_interest);
aRow += '</td>';
aRow += '<td width="20%">';
aRow += '$' + _this.formatNumber(_total);
aRow += '</td>';
aRow += '</tr>';
$('#amortizationChart tbody').append(aRow);
}
}, 

1 个答案:

答案 0 :(得分:1)

好的,我可以看到index.js _total中的第133行确实不是整数:

_total = _total - _principal;

如果你想得到一个整数,你可以这样做:

_total = Math.round(_total - _principal);

或者,如果您想获得特定数量的小数,请参阅此帖子:Formatting a number with exactly two decimals in JavaScript

这是你剩下的意思吗?