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 (Anthony Webster)
  • Subject: [Javascript] Rounding to 2 decimal places
  • Date: Mon, 22 Apr 2002 09:11:12 +0100

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@xxxxxxxxxxxx]
Sent: 20 April 2002 16:40
To: javascript@xxxxxxxxxx
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 =3D interest rate
   // n =3D num. payments
   // s =3D loan amt
return (s*i/12*Math.pow(i/12+1,n))/(Math.pow(i/12+1,n)-1);
}
function ShowVal() {
   var i =3D eval(document.calc.rate.value);
   var n =3D eval(document.calc.time.value);
   var s =3D eval(document.calc.amt.value);
   document.calc.pymt.value=3DMath.round(Monthly(i, n, s));
   document.calc.total.value=3DMath.round(Monthly(i, n, s)*n);
}

Thanks,
Audra Seigars

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