johram pong johram pong - 1 month ago 6
Javascript Question

how to not display decimals on this

My script works ok but at times the results are like 100.6456489764 but i want to instead show like 100.64 or just 100 but i do not get how to fix this here is my bit of code

would appreciate your kind help and input in this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en-CA" lang="en-CA" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="Javascript">
function dosum()
{
var TD = document.temps.OD.value /document.temps.PV.value *100;
var MLTV = document.temps.MA.value /document.temps.PV.value *100
document.temps.LTV.value = TD + MLTV
}
</script>
<script type="text/javascript" language="javascript">
function display_amount(amount)
{
var currency = '$';

if(isNumeric(amount))
{
if(amount < 0)
return '-' + currency + Math.abs(Math.round(amount,2));
else
return currency + Math.round(amount, 2);
}
else
return amount;

}
</script>
<title>LTV</title>
</head>
<body>
<FORM NAME="temps">
<TABLE bgcolor="#CCCCCC" cellspacing="0" cellpadding="10" width="350">
<TR><TD WIDTH=153 bgcolor="blue">
<font color="yellow"><b>Property Value:</b></font></TD>
<TD WIDTH=153 bgcolor="#0fcf00">
<p style="margin-top: 0; margin-bottom: 0">
$<INPUT TYPE="TEXT" NAME="PV" onChange="dosum()" SIZE="8" VALUE=""></p></TD></TR>
<TR><TD WIDTH=153 bgcolor="green">
<font color="white"><b>Mortgage Balance:</b></font></TD>
<TD WIDTH=153 bgcolor="red">
$<INPUT NAME="MA" onChange="dosum()" SIZE="8" VALUE=""></TD></TR>
<TR><TD WIDTH=153 bgcolor="#ddd000">
<font color="green"><b>Additional Debts:</b></font></TD>
<TD WIDTH=153 bgcolor="orange">
$<INPUT NAME="OD" onChange="dosum()" SIZE="8" VALUE=""></TD></TR>
<TR><TD WIDTH=153 bgcolor="#333000"> <b><font color="#ffffff">
<INPUT TYPE="NUMBER" NAME="LTV" SIZE="10" readonly>%</font></b></TD>
<TD WIDTH=153 bgcolor="#333000">
<INPUT TYPE="submit" VALUE="Calculate"></div></TD>
</TR>
</TABLE>
</FORM>

</body>
</html>


thanks in advance

Answer

Use .toFixed(2) to round the value to 2 digits.

function dosum() {
    var TD = document.temps.OD.value / document.temps.PV.value * 100;
    var MLTV = document.temps.MA.value / document.temps.PV.value * 100
    document.temps.LTV.value = (TD + MLTV).toFixed(2);
}
Comments