-1

我试图让年租金显示为两位小数和租金/平方米。英尺也有两位小数。我已经添加了脚本以使这两个字段自动显示美元金额和逗号。

任何帮助,将不胜感激!

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

1 回答 1

0

可能有更好的解决方案。四舍五入到两位小数的 hacky 解决方案:

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

即,如果要将 10.678 舍入为两位数,则首先乘以 100:得到 1067.8,然后调用 Math.round 得到 1068,然后除以 100 得到 10.68,这是所需的答案。

于 2013-04-15T16:14:05.930 回答