johram pong johram pong - 1 year ago 62
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" "">
<html xml:lang="en-CA" lang="en-CA" xmlns="">
<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 type="text/javascript" language="javascript">
function display_amount(amount)
var currency = '$';

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

<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>


thanks in advance

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download