Ceil Math function

We can get the ceiling of a number by using match ceil() function. Or by using ceil math function we can get the smallest integer that is greater than or equal to given number. So by using this we can get the next higher integer of a given number. Here is the syntax.
Let us try with some examples.

document.write (Math.ceil(11.257)); // output 12
document.write ("<br>----<br>");

document.write (Math.ceil(1.56)); // output 2 
document.write ("<br>----<br>");

document.write (Math.ceil(-3.564));  // output -3
document.write ("<br>----<br>");

document.write (Math.ceil(-7.164)); // output -7
document.write ("<br>----<br>");

Not numeric or blank inputs

We will get output as NaN for all blank and non-numeric inputs.
document.write(Math.ceil()); // Output NaN 
document.write(Math.ceil('xyz')); // Output NaN 

Difference between ceil() and round()

Math.ceil() always returns next higher side integer, where Math.round can return integer value higher or lower than the input value.
Example :
document.write(Math.round(5.67)+'<br>'); // Output 6
document.write(Math.ceil(5.67)+'<br>'); // Output 6

document.write(Math.round(5.42)+'<br>'); // Output 5
document.write(Math.ceil(5.42)+'<br>'); // Output 6

document.write(Math.round(-5.67)+'<br>'); // Output -6
document.write(Math.ceil(-5.67)+'<br>'); // Output -5

document.write(Math.round(-5.42)+'<br>'); // Output -5
document.write(Math.ceil(-5.42)+'<br>'); // Output -5
Let us develop a script where users can enter any data and get the ceil value of it.

Demo of Math.ceil function

Here is the code
echo "<script type="text/javascript">
function disp_data(){
var a1 = document.getElementById('t1').value;
document.getElementById("a2").innerHTML= a1;

<input type=text name=t1 id='t1' value=-12.78><input type=button value='Display Ceil value' onClick='disp_data()';> 
<div id='a2'  style=" background-color: #c0f0c0; width:200" > Enter a number</div>";
You can read the php math ceil function
Next lower value of any decimal number
JavaScript Math Reference
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer