[Javascript] Rounding to 2 decimal places

Anthony Webster Anthonywebster at venda.com
Mon Apr 22 03:11:12 CDT 2002


hi audra,

I havent looked at your code because I'm not really a javascript person, but in theory you should do the following:

eg, 1.6897054

1. multiply your number by 100 (so you get 168.97054)
2. round to the nearest whole number - you mentioned you could do this (so you get 168)
3. divide by 100 (so you get 1.68)

any good?

anthony

-----Original Message-----
From: WolfSong [mailto:wolfsong at midcoast.com]
Sent: 20 April 2002 16:40
To: javascript at LaTech.edu
Subject: [Javascript] Rounding to 2 decimal places


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

_______________________________________________
Javascript mailing list
Javascript at LaTech.edu
https://lists.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list