-1

I am trying to get the Annual Rent displayed with two decimals and the rent/sq. ft. to have two decimals as well. I've already added the scripting to have these two fields automatically display dollar ammounts and commas.

Any help would be appreciated!

 <body>


<table border="1" width="620" style="width: 620px; height: 58px">
  <tr>
    <td width="82" align="center" height="16"><font face="Arial" size="2">Sq.
      Ft.</font></td>
    <td width="90" align="center" height="16"><font face="Arial" size="2">Annual
      Rent</font></td>
    <td align="center" height="16" style="width: 104px"><font face="Arial" size="2">&nbsp;
      Rent/Sq. Ft.</font></td>
  </tr>
  <tr>
    <td width="82" align="center"><font face="Arial" size="2">
    <input type="text" name="SF_Tenant1" id="sqft1" OnKeyUp="calcRentSQFTOne()"
  size="10" value="Sq. Ft.:" tabindex="602" class="style4"></font></td>
    <td width="90" align="center"><font face="Arial" size="2"><input type="text"   
   name="AnnualRent_Tenant1" id="annualrent1"  OnKeyUp="calcRentSQFTOne()" size="11" 
    value="Annual Rent:" tabindex="604"></font></td>
    <td align="center" style="width: 104px;"><font face="Arial" size="2">
<input type="text" name="RentSF_Tenant1" id="rentsqft1"  
 readonly="readonly"     size="11"      value="Rent/Sq. Ft.:"      tabindex="605"></font></td>
  </tr>
  </table>
 &nbsp;
  <input type="submit" 
  value="Save" name="Save" tabindex="999" style="font-family:     Arial;            
  font-size: 10pt; width: 65px; height: 29px;"></font></p>
    <script type="text/javascript">
    //calculation script
    function calcRentSQFTOne(){
    SquareFeet1 = document.getElementById("sqft1").value;
    AnnualRent1 = document.getElementById("annualrent1").value;

    document.getElementById("rentsqft1").value =  
                    (AnnualRent1 * 1) 
                                          / (SquareFeet1 * 1);


 }
 //Dollar format
 function formatNumber(number, digits, decimalPlaces, withCommas)
 {
    number       = number.toString();
var simpleNumber = '';

// Strips out the dollar sign and commas.
for (var i = 0; i < number.length; ++i)
{
    if ("0123456789.".indexOf(number.charAt(i)) >= 0)
        simpleNumber += number.charAt(i);
}

number = parseFloat(simpleNumber);

if (isNaN(number))      number     = 0;
if (withCommas == null) withCommas = false;
if (digits     == 0)    digits     = 1;

var integerPart = (decimalPlaces > 0 ? Math.floor(number) : Math.round(number));
var string      = "";

for (var i = 0; i < digits || integerPart > 0; ++i)
{
    // Insert a comma every three digits.
    if (withCommas && string.match(/^\d\d\d/))
        string = "," + string;

    string      = (integerPart % 10) + string;
    integerPart = Math.floor(integerPart / 10);
}

if (decimalPlaces > 0)
{
    number -= Math.floor(number);
    number *= Math.pow(10, decimalPlaces);

    string += "." + formatNumber(number, decimalPlaces, 0);
 }

 return string;
 }



 </script>

 </body>

1 Answers1

0

Likely there is a better solution. A hacky solution to round to two decimal digits:

return Math.round(input*100)/100;

I.e. if you want to round 10.678 to two digits, then first multiply by 100: you get 1067.8, then call Math.round to get 1068 then divide by 100 to get 10.68, which is the desired answer.

Palo
  • 1,051
  • 8
  • 12