This JavaScript tutorial explains how to use the math function called ceil() with syntax and examples.
Description
In JavaScript, ceil() is a function that is used to return the smallest integer value that is greater than or equal to a number. In other words, the ceil() function rounds a number up and returns an integer value. Because the ceil() function is a static function of the Math object, it must be invoked through the placeholder object called Math.
Syntax
In JavaScript, the syntax for the ceil() function is:
Math.ceil(number);Parameters or Arguments
numberThe number used to find the smallest integer.Returns
The ceil() function returns the smallest integer value that is greater than or equal to a number.
Note
- Math is a placeholder object that contains mathematical functions and constants of which ceil() is one of these functions.
Example
Let's take a look at an example of how to use the ceil() function in JavaScript.
For example:
console.log(Math.ceil(32.65)); console.log(Math.ceil(8.1)); console.log(Math.ceil(-4.2));In this example, we have invoked the ceil() function using the Math class.
We have written the output of the ceil() function to the web browser console log, for demonstration purposes, to show what the ceil() function returns.
The following will be output to the web browser console log:
33 9 -4In this example, the first output to the console log returned 33 which is 32.65 rounded up to an integer value.
The second output to the console log returned 9 which is 8.1 rounded up to an integer value.
The third output to the console log returned -4 which is -4.2 rounded up to an integer value. Notice that when dealing with negative numbers, the ceil() function rounds up towards zero and in this case, returned -4 and not -5.
Returns the smallest integer greater than or equal to the given value. The result is a decimal data type.Syntax
ceil(X)Input
ArgumentTypeDescriptionXany numeric typeA numeric value
Return Value
Returns a decimal value corresponding to the smallest integer greater than or equal to X.
If X is N/A, the result is N/A.
Sample Usage
<table cols="int_value,dec_value" types="i,f"> 1,-5.25; 2,5.0; 3,13; 4,4.5; 5,5.0; </table> <willbe name="ceil_int_example" value="ceil(int_value)"/> <willbe name="ceil_dec_example" value="ceil(dec_value)"/> <colord cols="int_value,ceil_int_example,dec_value,ceil_dec_example"/>Example
In this example, the ceil(X) function returns the smallest integer greater than or equal to the given value of the <table cols="int_value,dec_value" types="i,f"> 1,-5.25; 2,5.0; 3,13; 4,4.5; 5,5.0; </table> <willbe name="ceil_int_example" value="ceil(int_value)"/> <willbe name="ceil_dec_example" value="ceil(dec_value)"/> <colord cols="int_value,ceil_int_example,dec_value,ceil_dec_example"/>1 column on the <table cols="int_value,dec_value" types="i,f"> 1,-5.25; 2,5.0; 3,13; 4,4.5; 5,5.0; </table> <willbe name="ceil_int_example" value="ceil(int_value)"/> <willbe name="ceil_dec_example" value="ceil(dec_value)"/> <colord cols="int_value,ceil_int_example,dec_value,ceil_dec_example"/>2 table. The new column contains decimal data type values.
The Math.ceil() function always rounds up and returns the smaller integer greater than or equal to a given number.
Try it
Syntax
Math.ceil(x)
Parameters
x
A number.
Return value
The smallest integer greater than or equal to x. It's the same value as -Math.floor(-x).
Description
Because ceil() is a static method of Math, you always use it as Math.ceil(), rather than as a method of a Math object you created (Math is not a constructor).