Reputation: 2785
I have created calculator to find Term of loan based on loan amount, Interest rate per annum and Monthly payment.
My Example Data is:
Loan amount: 1,000,000
Interest Rate: 1.09
Monthly Payment: 1,500
With this example data, my result is 85.39yr(s) and answer is right but when enter 10,000,000.00 i am getting 0.00 result. Why i am getting this result and what's wrong in my code.
Any ideas or suggestions? Thanks.
My Code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Term of loan calculator</title>
</head>
<body>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery('#frmCalculate input[type="text"]').on('change', function() {
var val = this.value;
val = val.replace(/,/g, "");
val = number_format(val, 2);
this.value = val;
});
});
//function for formatting number with commas
function number_format(number, decimals, dec_point, thousands_sep) {
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);
}
function calculate() {
//Term of loan
if (frmCalculate.loan_amount.value != "" && frmCalculate.interest_rate.value != "" && frmCalculate.monthly_payment.value != "") {
var loan_amount = document.getElementById("loan_amount").value;
loan_amount = loan_amount.replace(/,/g, "");
var interest_rate = document.getElementById("interest_rate").value;
interest_rate = interest_rate.replace(/,/g, "");
var monthly_payment = document.getElementById("monthly_payment").value;
monthly_payment = monthly_payment.replace(/,/g, "");
loan_amount = parseFloat(loan_amount.replace(/\s/g, "").replace(",", "."));
monthly_payment = parseFloat(monthly_payment.replace(/\s/g, "").replace(",", "."));
rte = interest_rate / 1200;
pmt = monthly_payment;
amt = loan_amount;
yrs = (Math.log(1 + (rte / (pmt / (amt) - rte))) / Math.log(1 + rte)) / 12;
yrs = yrs * 1000;
yrs = Math.round(yrs);
yrs = yrs / 1000;
document.getElementById('term_value').innerHTML = number_format(yrs, 2);
}
}
</script>
<form action="#" method="post" id="frmCalculate" name="frmCalculate">
<div class="user-prf-box">
<div class="user-prf-inner-bg">
<!--Loan amount-->
<div class="mortage-div-row">
<label class="loan_label" id="lbl_amount" for="loan_amount">
Loan amount
</label>
<span>
<small class="small">
S$
</small>
<input type="text" onchange="customCheck()" class="nput-calc amount_box" id="loan_amount" name="loan_amount">
<div id="amount_value">
</div>
</span>
</div>
<!--Interest rate per annum-->
<div class="mortage-div-row">
<label class="lineheight-normal loan_label" id="lbl_interest" for="interest_rate">
Interest rate per annum
</label>
<span style="line-height:25px;">
<input type="text" maxlength="12" class="input-calc new-input interest_box" id="interest_rate" name="interest_rate" style="display: inline;">
<span id="interest_value">
</span>
<span class="percentage">
%
</span>
</span>
</div>
<!--Term of loan-->
<div class="mortage-div-row">
<label class="loan_label" id="lbl_term" for="term_loan">
Term of loan
</label>
<span style="line-height:25px;">
<input type="text" maxlength="12" class="input-calc new-input term_box" id="term_loan" name="term_loan" style="display: none;" disabled="disabled">
<b><div id="term_value">
</div></b>
<span class="yrcs">
yr(s)
</span>
</span>
</div>
<!--Monthly payment-->
<div style="line-height:25px;" class="mortage-div-row">
<label class="lbl_monthly" id="lbl_monthly_payment">
Monthly payment
</label>
<span class="width-diff">
<span class="lbl_monthly">
S$
<span id="monthly_payment_value">
</span>
<span id="total">
</span>
</span>
<input type="text" value="" class="input-calc new-input monthly_payment_box" id="monthly_payment" name="monthly_payment" style="display: inline; margin-left: 2px;">
</span>
</div>
<div class="btn-regis-div">
<input type="button" onclick="calculate();" value="CALCULATE" class="btnsavedrft calculate" name="loancal">
</div>
</div>
</div>
</form>
</body>
</html>
Upvotes: 1
Views: 2151
Reputation: 4508
With a rate of 1.09% and a loan of $1M, the monthly interest is
1.09/12/100*10^6=908.33
When the loan is $10M, the monthly interest is $9,083.33. Since the monthly payment doesn't cover the interest, the balance of the loan grows, and the loan is never repaid. Your monthly payment must cover the interest accrued, and then some.
Upvotes: 3
Reputation: 6871
You calculation wants Math.log to sometimes calculate a negative number. Since you can take the log of a negative you get an error.
check out the fiddle. I put console.log
in strategic places. Note that this calculation:
pmt / (amt) - rte
gives a negative output.
http://jsfiddle.net/raam86/azATg/
Upvotes: 2