Click to See Complete Forum and Search --> : help with rounding numbers


kalibballer
07-29-2003, 02:34 AM
how would have the calculated answer rounded to 2 decimal places

<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 * 12;
var intr = document.calc.rate.value / 1200;
document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
}

// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))

}

// -->

</script>

Gollum
07-29-2003, 02:37 AM
Math.round(x) rounds a number (x) to the nearest integer, so Math.round(x*100)/100 will round it to two decimal places.

kalibballer
07-29-2003, 02:47 AM
i am getting errors where do i insert the
Math.round(x)
Math.round(x*100)/100

Gollum
07-29-2003, 03:08 AM
To make things easier you could add a function...

function RoundToTwo(x)
{
return Math.round(x*100)/100;
}


and then when you want to round a number you can use it like this...


payment = RoundToTwo(principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months)));

Charles
07-29-2003, 03:45 AM
To make things even easier you can use the 'Number.toFixed()' method.

alert(Math.PI.toFixed(2))