[Javascript] Rounding to 2 decimal places

WolfSong wolfsong at midcoast.com
Sat Apr 20 10:40:19 CDT 2002


Hi All,

I'm working on a simple mortgage calculator for my CIS class and I've
figured out how to round a number, but how to I round the number to 2
decimal places instead of a whole number? I know this is question is very
newbie, but I just can't figure it out without getting an error or throwing
the correct payments, etc. all out of wack!

This is the code I've been working on:

function Monthly(i, n, s) {
   // i = interest rate
   // n = num. payments
   // s = loan amt
return (s*i/12*Math.pow(i/12+1,n))/(Math.pow(i/12+1,n)-1);
}
function ShowVal() {
   var i = eval(document.calc.rate.value);
   var n = eval(document.calc.time.value);
   var s = eval(document.calc.amt.value);
   document.calc.pymt.value=Math.round(Monthly(i, n, s));
   document.calc.total.value=Math.round(Monthly(i, n, s)*n);
}

Thanks,
Audra Seigars




More information about the Javascript mailing list