Tutorialspoint.dev

JavaScript | Math.ceil( ) function

The Math.ceil() function in JavaScript is used to round the number passed as parameter to its nearest integer in Upward direction of rounding i.e towards the greater value.

Syntax

Math.ceil(value)
Parameters :
The number to be rounded to its
nearest integer in upward rounding method.
Returns :
Result after rounding the number passed
as parameter to the function.
passed as parameter.

Example :

Input  : Math.ceil(4.23)
Output : 5

Input  : Math.ceil(0.8)
Output : 1

Errors and Exceptions
1. A non-numeric string passed as parameter returns NaN
2. An array with more than 1 integer passed as parameter returns NaN
3. An empty variable passed as parameter returns NaN
4. An empty string passed as parameter returns NaN
5. An empty array passed as parameter returns NaN

Below are some examples that illustrate the Math.ceil() function in JavaScript:



<!-- NEGATIVE NUMBER EXAMPLE -->
<script type="text/javascript">
    document.write(Math.ceil(-2)); 
    document.write(Math.ceil(-2.56));          
</script>

Output:

-2
-2
<!-- POSITIVE NUMBER EXAMPLE -->
<script type="text/javascript">
    document.write(Math.ceil(2)); 
    document.write(Math.ceil(2.56));          
</script>

Output:

2
3
<!-- STRING EXAMPLE -->
<script type="text/javascript">
    document.write(Math.ceil("Geeksforgeeks"));          
</script>

Output:

NaN
<!-- ADDITION INSIDE FUNCTION EXAMPLE -->
<script type="text/javascript">
    document.write(Math.ceil(7+9));           
</script>

Output:

16


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter