Introduction to Ceil() in JavaScript
Ceil function is a math function where a number is to be passed as a parameter and then used to round off a number in JavaScript. This number will be the nearest integer in the upward direction of the number sent as a parameter to the function which will result in a higher value to serve as a ceil to the number sent.
Syntax of Ceil() in JavaScript
The syntax of the round function is as follows:
ceil(x)
This is a user-defined function that accepts a single parameter. This is the number which will be rounded off to the upper integer nearest to x. This function can be interpreted as:
Math.ceil(x)
Here Math is a predefined Javascript object. Ceil is used to get the value of the number which needs to be rounded off to the upward value of the nearest integer. This function is apparently a static method and hence is always used as math.ceil (x). The return value or the result will be the number which is the nearest integer to the given number.
Examples of Ceil() in JavaScript
Let us now have a look at a few examples and see how the ceil function works.
Example #1
console.log (Math.ceil(.95));
// expected output: 1
Example #2
console.log (Math.ceil(4));
// expected output: 4
Example #3
console.log (Math.ceil(7.004));
// expected output: 8
Example #4
console.log (Math.ceil(-7.004));
// expected output: -7
The above are four cases where the ceil method is being used. Let us go through each of them and understand how this function is working.
In the first example, the ceil function is used with a decimal number of 0.95. The upper integer of this number is 1. As a result, the output for this function will be 1. We will see this in the screenshot which will follow this explanation.
The second example has the number as integer 4. This integer is being used with the math.ceil function. As per the working of this function, if the argument which is passed to the function is an integer then that value which is passed will not be rounded off. The second example hence will have the same output as the input that is 4.
Let us move ahead to the third example where we have a decimal number 7.004 being passed as an argument to the ceil function. As we know that ceil takes the upper limit of the decimal that is the number which is an integer higher to the decimal. Hence the result for this will be 8. 8 being the next higher number.
In a similar way, the fourth example mentioned takes a negative decimal value. This value is -7.004. As per the working of ceil function, the upper limit for negative numbers is the previous number. In this case, the upper limit integer will be -7. Hence the output of ceil function will always be the number itself as that number will be the upper limit.
You can check the results of all the above examples which when run provide the below results.
Now let us take an example where a string is being sent as an argument to the ceil function.
Example #5
Code:
<!-- STRING EXAMPLE -->
<script type="text/javascript">
document.write (Math.ceil ("I am String"));
</script>
This code explains to us what happens when a string is sent as an argument to the ceil function. The result of ceil function when used with string returns a null value. It is not able to handle the string value and hence returns a null. The below screenshot explains what the above code gives as a result.
Output:
Example #6
Now, let us take an example where you will see that addition is being done inside the ceil function. What result will you expect when an addition operation takes place within this function. You can use the below code to check this functionality
Code:
<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
<script type="text/javascript">
document.write (Math.ceil (7 + 9));
</script>
The example has taken two integers which are being added up in the ceil function. The result of the above code will be an integer. This integer is 16. These examples hence let us know that the addition of integers is possible in the ceil function. The below screenshot is the code with its result.
Output:
Now let us make a slight change in our above code. Instead of an integer let us take one number which is a decimal number. We can change from 7 to 7.2. The addition of 7.2 and 9 is 16.2. As per the working of ceil function the upper limit of 16.2 is 17. Hence the result should be 17. The below screenshot will display that the addition of decimal and integer is also supported by the ceil function in JavaScript.
<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
<script type="text/javascript">
document.write (Math.ceil (7.2 + 9));
</script>
Output:
Example #7
Let us take a final example here where 0 is being sent as an argument to the ceil function.
Code:
<script type="text/javascript">
document.write ("Output : " + Math.ceil(0));
</script>
The ceil function here returns the output as 0. This is because 0 is also an integer and as per the working of the function when an integer is passed as an argument the return value will be the integer itself. Hence the result here will be a 0. The below screenshot will give a glimpse of the above example.
Output:
When a null value is passed as a parameter to this function then the return value here is a 0 as well.
Ceil function is supported by Chrome, Safari, Internet Explorer and Firefox web browsers.
Conclusion
The ceil function is a way of rounding off a decimal to the next bigger integer value. Whenever a decimal value is sent the upward value to that decimal will be the output when this function is used. It is a quick way of rounding off numbers to the next bigger value. The ceil function can also be combined with HTML and can hence assist in getting the element value or position of your mouse. Hence ceil function comes in the Math library of JavaScript and enables a programmer to work easily with numbers.