Jun 5, 2012
72 Views

Tính tiền trả góp

Written by

CSS

<style type=”text/css”>

body{
background-color:white;
font-size:10pt;
color:black;
font-family:sans-serif;
}
</style>

JAVASCRIPT

<script language=”javascript”>
var amount, apr, n, payment, npy, tablebuilt=false;

function calculate(){
var d=document.f;
amount=d.amount.value;
apr=d.apr.value;
n=d.n.value;
npy=d.npy.value;
if( (amount!=”) && (n!=”) && (apr!=”) && (npy!=”) ){
tmp=Math.pow((1+(apr/100/npy)), (n*npy));
payment=(amount*tmp*(apr/100/npy))/(tmp-1);
if((!isNaN(payment))&&(payment!=Number.POSITIVE_INFINITY)&&(payment!=Number.NEGATIVE_INFINITY)){
d.payment.value=round(payment);
d.totpaid.value=round(payment*n*npy);
d.intpaid.value=round((payment*n*npy)-amount);
}else alert(‘Error:\nOne or more fields contain data\nwhich cannot be used in the\ncalculation.’);
}else alert(‘Error:\nYou did not provide enough data.’);
}

function round(val){
tmp=Math.round(val*100)/100+”;
if(tmp.indexOf(‘.’)==-1)tmp+=’.00′;
else if(tmp.length-tmp.indexOf(‘.’)==2)tmp+=’0′;
return tmp;
}

function resetall(){
var d=document.f;
d.amount.value=”;
d.apr.value=”;
d.n.value=”;
d.npy.value=12;
d.payment.value=”;
d.totpaid.value=”;
d.intpaid.value=”;
d.amount.focus();
}

function buildtable(){
tablebuilt=true;
txt='<html><head><title>Payment Schedule</title></head><body bgcolor=”#00436e”><center><form>’;
txt+='<input type=”button” value=”Close” onClick=”self.close()”><br><br>’;
txt+='<table bgcolor=”white” border=”1″ cellpadding=”4″>’;
txt+='<tr><td align=”center”>Payment<br>Number</td><td align=”center”>Payment<br>Amount</td><td align=”center”>Interest</td><td align=”center”>Principle</td><td align=”center”>Balance</td></tr>’;
amount=eval(amount);
for(i=1;i<=n*npy;i++){
tbldata='<td bgcolor=”‘+((i%2!=0) ? ‘lightgrey’ : ‘white’)+'” align=”right”>’;
interest=amount*apr/npy/100;
amount+=interest;
principle=payment-interest;
amount-=payment;
txt+='<tr>’+tbldata+i+’:</td>’+tbldata+round(payment)+'</td>’+tbldata+round(interest)+'</td>’+tbldata+round(principle)+'</td>’+tbldata+round(amount)+'</td></tr>’;
}
txt+='</table><br><br><input type=”button” value=”Close” onClick=”self.close()”></center></form></body></html>’;
var psch=window.open(”, ‘viewsch’ ,’top=0,left=0,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,menubar=no,status=no’);
psch.document.write(txt);
}

window.onload=function(){
document.f.amount.focus();
}
</script>

HTML

<form name=”f”>
<table border=”1″ cellpadding=”4″><tr><td><table border=”0″>
<tr><td colspan=”2″ align=”center”><b>Enter Loan Data Here</b></td></tr>
<tr><td width=”400″>Loan Amount:</td><td><b>$</b> <input type=”text” name=”amount” size=”10″></td></tr>
<tr><td>Annual Percentage Rate (APR) %:</td><td><input type=”text” name=”apr” size=”5″><b>
%</b></td></tr>

<tr><td>Repayment period in years:</td><td><input type=”text” name=”n” size=”5″></td></tr>
<tr><td>Amount of payments per year:</td><td><input type=”text” name=”npy” size=”5″ value=”12″></td></tr>
<tr><td colspan=”2″>&nbsp;</td></tr>
<tr><td align=”center” colspan=”2″><input type=”button” value=”Calculate” onClick=”calculate()”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=”button” value=”Reset” onClick=”resetall()”></td></tr>
<tr><td colspan=”2″><hr></td></tr>
<tr><td>Amount per payment:</td><td><b>$</b> <input type=”text” size=”10″ name=”payment”></td></tr>
<tr><td>Total paid over the life of the loan:</td><td><b>$</b> <input type=”text” size=”10″ name=”totpaid”></td></tr>
<tr><td>Total interest paid over the life of the loan:</td><td><b>$</b> <input type=”text” size=”10″ name=”intpaid”></td></tr>

<tr><td colspan=”2″>&nbsp;</td></tr>
<tr><td colspan=”2″ align=”center”><input type=”button” value=”Build Payment Schedule” onClick=”buildtable()”><br><br><small>
(Please give the script time to work, especially if the loan is for a long
time.)</small></td></tr>
</table></td></tr></table>
</form>

Article Tags:
· ·
Article Categories:
Code/Web
    http://linholiver.com

    https://linholiver.com/diary/about/