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

How to I round up or down the results in "nooftut" Calculation : studentenrolled / class size = nooftut As studentenrolled is pulled from the db, I'm not able to round up or down

Jeffrey
  • 15
  • 3

2 Answers2

0
+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
demkovych
  • 7,827
  • 3
  • 19
  • 25
0

Try 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>
Yahiya
  • 761
  • 4
  • 15
  • Hello, Thank you so much. I was able to see the result however I'm not able to post to result into another page. – Jeffrey Oct 24 '19 at 12:57