2013-11-02 92 views
0

我得到這個幾乎工作,但由於某種原因餘額圖上有餘額。我認爲這可能是一個舍入問題,但我不確定。如果有人有這方面的經驗,你可以看看每月支付功能。這是一個小Backbone.js的應用我開發的一部分,你可以看到它在行動http://rvb.chrismills.lajavascript抵押貸款計算器功能

// 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); 
} 
}, 
+0

在jsfiddle.net中創建一個實時演示 – charlietfl

+0

您能更準確地指出哪個變量有額外的餘數嗎? – pax162

+0

或者你可以點擊鏈接,所以我不必將所有的js文件編譯成js小提琴。 – user1572796

回答

1

OK,我能在index.js _total看到,在第133行確實不是一個整數:

_total = _total - _principal; 

如果你想獲得一個整數,你可以這樣做:

_total = Math.round(_total - _principal); 

或者,如果你想獲得小數的具體數量,看到這個帖子:Formatting a number with exactly two decimals in JavaScript

這是你剩餘的意思嗎?

+0

我會試試看,謝謝。 – user1572796