-1

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

  • So, the `font` element has been deprecated for a long time... Besides that, look at [`toFixed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed). – Heretic Monkey Oct 17 '16 at 18:23
  • 2
    similar issue: http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript – roger Oct 17 '16 at 18:24

3 Answers3

1

Use

Math.round(num * 100) / 100 

Or something like this

var discount = (price / listprice).toFixed(2);
Neo
  • 3,309
  • 7
  • 35
  • 44
1

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);
}
Ram
  • 504
  • 3
  • 11
0

For 2 digits precision use toFixed()

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);
}      
Qrchack
  • 899
  • 1
  • 10
  • 20