2013-04-15 35 views
-1

我想要顯示帶有兩位小數的年度租金和租金/平方。英尺也有兩位小數。我已經添加了腳本來讓這兩個字段自動顯示美元ammounts和逗號。年租金除以平方英尺顯示兩位小數點。 (即1,000.38)

任何幫助,將不勝感激!

<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 (".".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> 

回答

0

可能有更好的解決方案。哈希解決方案,以四捨五入到兩位十進制數字:

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

I.e.如果要將10.678舍入爲兩位數,則首先乘以100:得到1067.8,然後調用Math.round得到1068,然後除以100得到10.68,這是所需的答案。

相關問題