我如何对结果进行四舍五入?

<script >

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 = result;

    }

}

</script>


         <tr>

         <td><label for="studentenrolled">Student Enrolled</label>  </td>

        <td> <input type="text" name="studentenrolled" id="studentenrolled"  value="<?php echo $row['studentenrolled']; ?> " /></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> 

如何在“nooftut”计算中向上或向下舍入结果:studentenrolled / class size = nooftut 由于 studentenrolled 是从数据库中提取的,我无法向上或向下舍入


不负相思意
浏览 130回答 2
2回答

神不在的星期二

尝试 Math.round()function sum() {&nbsp; &nbsp; var txtFirstNumberValue = document.getElementById('studentenrolled').value;&nbsp; &nbsp; var txtSecondNumberValue = document.getElementById('classsize').value;&nbsp; &nbsp; var result = parseInt(txtFirstNumberValue) / parseInt(txtSecondNumberValue);&nbsp; &nbsp; if (!isNaN(result)) {&nbsp; &nbsp; &nbsp; &nbsp;document.getElementById('nooftut').value = Math.round(result);&nbsp; &nbsp; }}<tr>&nbsp; &nbsp;<td><label for="studentenrolled">Student Enrolled</label>&nbsp; </td>&nbsp; &nbsp;<td> <input type="text" name="studentenrolled" id="studentenrolled" value=" " /></td></tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<tr>&nbsp; &nbsp;<td><label for="classsize">Class Size</label>&nbsp; </td>&nbsp; &nbsp;<td> <input type="text" id="classsize"&nbsp; onkeyup="sum();" /></td></tr>&nbsp;<tr>&nbsp; &nbsp;<td><label for="nooftut">No Of Tutorial</label>&nbsp; </td>&nbsp; &nbsp;<td> <input type="text" id="nooftut" disabled="disabled" /></td></tr>

慕码人2483693

+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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript