I am trying to get my outputs to round to two decimals places and have them formatted as a currency ie $23,923.23. I am unfortunately struggling to get it to work. I am very new to this and have tried googling and searching on here for an answer but to no avail. Any ideas?
<script language="JavaScript">
<!--
function showpay() {
if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) ||
(document.calc.months.value == null || document.calc.months.value.length == 0) ||
(document.calc.rate.value == null || document.calc.rate.value.length == 0)) {
document.calc.pay.value = "Incomplete data";
} else {
var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
var intr2 = document.calc.rate2.value / 1200;
document.calc.pay.value = princ * intr / (1 - (Math.pow(1 / (1 + intr), term)));
document.calc.pay2.value = princ * intr2 / (1 - (Math.pow(1 / (1 + intr2), term)));
var month1 = document.calc.pay.value;
var month2 = document.calc.pay2.value;
document.calc.difference.value = month1 - month2;
var difference = document.calc.difference.value;
document.calc.total.value = difference * term;
var intr3 = document.calc.rate3.value / 1200 + 1;
var intr4 = Math.pow(intr3, term)
document.calc.portfolio.value = difference * ((intr4 - 1) / (intr3 - 1));
}
// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))
}
// -->
</script>
<form name=calc method=POST>
<table width=100% border=0>
<tr>
<th bgcolor="#aaaaaa" align=center>
<font color=blue>Student Loan Refinance Calculator</font>
</th>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Loan Amount <input type=number name=loan style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Loan Length in Months <input type=number name=months style="width: 75px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Old Interest Rate (7%=7) <input type=number name=rate style="width: 50px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>New Interest Rate <input type=number name=rate2 style="width: 50px"></td>
</tr>
<tr>
<td bgcolor="#ffffff" align=center><input type=button onClick='showpay()' value=Calculate></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Old Monthly Payment <input type=number name=pay style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>New Monthly Payment <input type=number name=pay2 style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Monthly Savings <input type=number name=difference style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Total Savings <input type=number name=total style="width: 100px"></td>
</tr>
<tr>
<th bgcolor="#aaaaaa" width=100%>
<font color=blue>If you invest the monthly savings</font>
</th>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Annual Return (7%=7) <input type=number name=rate3 style="width: 50px"></td>
</tr>
<tr>
<td bgcolor="#ffffff" align=center><input type=button onClick='showpay()' value=Calculate></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Portfolio Value <input type=number name=portfolio style="width: 100px"></td>
</tr>
</table>
</form>