2 回答
TA贡献1963条经验 获得超6个赞
尝试 Math.round()
function sum() {
var txtFirstNumberValue = document.getElementById('studentenrolled').value;
var txtSecondNumberValue = document.getElementById('classsize').value;
var result = parseInt(txtFirstNumberValue) / parseInt(txtSecondNumberValue);
if (!isNaN(result)) {
document.getElementById('nooftut').value = Math.round(result);
}
}
<tr>
<td><label for="studentenrolled">Student Enrolled</label> </td>
<td> <input type="text" name="studentenrolled" id="studentenrolled" value=" " /></td>
</tr>
<tr>
<td><label for="classsize">Class Size</label> </td>
<td> <input type="text" id="classsize" onkeyup="sum();" /></td>
</tr>
<tr>
<td><label for="nooftut">No Of Tutorial</label> </td>
<td> <input type="text" id="nooftut" disabled="disabled" /></td>
</tr>
TA贡献1860条经验 获得超9个赞
+1.5 => +1.0 using Math.floor() (To round down)
-1.5 => -1.0 using Math.ceil() (To round up)
or just Math.round(0.9) => 1
添加回答
举报