The syntax of the round function is as follows:
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:
Let us now have a look at a few examples and see how the ceil function works.
console.log (Math.ceil(.95)); // expected output: 1
console.log (Math.ceil(4)); // expected output: 4
console.log (Math.ceil(7.004)); // expected output: 8
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.
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.
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
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.
Let us take a final example here where 0 is being sent as an argument to the ceil function.
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.
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.