年間家賃を小数点以下 2 桁と家賃/平方で表示しようとしています。ft. 小数点以下も 2 桁です。これら 2 つのフィールドに金額とカンマが自動的に表示されるようにスクリプトを追加しました。
どんな助けでも大歓迎です!
<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">
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>
<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>