Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Rounding to 2 decimal places


  • From: javascript@xxxxxxxxxx (WolfSong)
  • Subject: [Javascript] Rounding to 2 decimal places
  • Date: Sat, 20 Apr 2002 11:40:19 -0400

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