JavaScript ceil

The JavaScript ceil function is a Math function that returns the smallest integer value, which is greater than or equal to the specified expression or a number. It always rounds towards positive infinity. For instance, Math.ceil(1.2) is 2.

JavaScript ceil() syntax

The syntax of the Math.ceil() function is

Math.ceil(number);

Parameters: This function accepts a single parameter (number) to perform ceiling.

Return Value: The JavaScript Math.ceil() function returns the nearest integer moving upwards. In short, it returns the smallest integer greater than or equal to the given number argument.

  • If the number argument is a positive or negative number, it will return the ceiling value.
  • If the specified number argument is not a number, it will return NaN.
  • And if it is Null, it returns zero.

TIP: To utilize the function, we must use Math.ceil() because ceil() is a static method of Math.

JavaScript ceil Example

In this section, we will explain the Math.ceil() function behaviour on positive, negative, zero, string, and other values.

ceil() function on positive floating-point numbers

As we mentioned earlier, when we pass a positive floating-point number, the Math.ceil() function rounds it to the nearest integer value. It moves upwards towards positive infinity.

For instance, if we pass positive integer or floating-point numbers 20.01 or 20.88, the ceil() function will return 21 as output.

console.log(Math.ceil(20.01));
console.log(Math.ceil(20.88));
21
21

However, when we pass the integer value 20 as the input, the ceil() function returns the same value (20) as output.

console.log(Math.ceil(20));
20

JavaScript ceil with the Negative Numbers

When we pass a negative floating-point number or integer to the ceil() function, the rounded value will move towards zero (towards the positive direction). For instance, if we pass negative values, such as -10.50 or -10, the ceil() function will return -10 as the output. Remember, -10 is greater than -10.50.

console.log(Math.ceil(-10));
console.log(Math.ceil(-10.98));
-10
-10

Rounding the Numeric string

When we pass a numeric string (it’s basically a number in string format), the Math.ceil() function automatically converts the string data type to a float and returns the ceiling value.

const s = "50.895"
console.log(Math.ceil(s))
51

ceil 0

If we use the Math.ceil() function with a positive or negative 0, it returns the same output.

console.log(Math.ceil(0));
console.log(Math.ceil(-0));
0
-0

JavaScript Math.ceil() function example

In this example, we will use this one to find the ceiling values of different data types and display the output.

  1. Within the first two statements, we used it directly on both the positive and negative integers.
  2. Following four statements, we used the ceil Function on both the Positive and negative Decimal values.
  3. Next, we tried on the String value. As we said before, this will return NaN as output.
  4. Last, we tried this Math function on the Null value, and JavaScript returns zero as output.
<!DOCTYPE html>
<html>
<head>
    <title> JavaScriptCEILFunction </title>
</head>
<body>
  <h1> JavaScriptCEILFunction </h1>
  <p id = "Pos"></p>
  <p id = "Neg"></p>
  <p id = "Dec"></p>
  <p id = "Dec1"></p>
  <p id = "Neg_Dec"></p>
  <p id = "Neg_Dec1"></p>
  <p id = "Str"></p>
  <p id = "Exp"></p>
  <p id = "Null"></p>
  <script>
    document.getElementById("Pos").innerHTML = Math.ceil(20);
    document.getElementById("Neg").innerHTML = Math.ceil(-20);
    document.getElementById("Dec").innerHTML = Math.ceil(10.05);
    document.getElementById("Dec1").innerHTML = Math.ceil(10.95);
    document.getElementById("Neg_Dec").innerHTML = Math.ceil(-6.05);
    document.getElementById("Neg_Dec1").innerHTML = Math.ceil(-6.95);
    document.getElementById("Str").innerHTML = Math.ceil("JavaScript");
    document.getElementById("Null").innerHTML = Math.ceil(null);
  </script>
</body>
</html>
JavaScript ceil function example

Using ceil() with Infinity and NaN

For positive or negative infinity and nan values, the ceil() function returns the same output.

console.log(Math.ceil(Infinity));
console.log(Math.ceil(-Infinity));
console.log(Math.ceil(NaN));
Infinity
-Infinity
NaN

JavaScript ceil array

Apart from individual numbers, we can use the Math.ceil() function on array items to round the array elements to the nearest integer. Here, the map() helps to create a new array by applying the ceil() function to individual array items.

const numbers = [10.25, 25.89, 30.78, 44.01, 59.10];

const res = numbers.map(Math.ceil);
console.log(res);
[ 11, 26, 31, 45, 60 ]

Apply ceiling on nested arrays

Similar to the above, we can use the map() function to apply the ceil() function on nested array items.

const numbers = [[10.25, 25.89], [30.78, 44.01], [59.10, 69.3, 99.2]];

const res = numbers.map(r => r.map(Math.ceil));
console.log(res);
[ [ 11, 26 ], [ 31, 45 ], [ 60, 70, 100 ] ]

JavaScript ceil() function: Real-time examples

What is the ceil of 2?

2. As it is an integer, the Math.ceil() function returns the same value as output.

How to round 2.2 to 3 in JS?

We can use the Math.ceil() function, which rounds a floating-point number to the next integer.

console.log(Math.ceil(2.2));
3

Ceiling floating-point numbers to decimal precision

By default, the Math.ceil() function returns an integer. However, we can perform mathematical calculations to round the decimal precision. Instead of the ** operator, we can use the pow() function. To round downwards, use the floor() function.

function decimalPrecision(n, precision = 0)
{
  const dec = 10 ** precision;
  return Math.ceil((n + Number.EPSILON) * dec) / dec;
}

console.log(decimalPrecision(300.35259, 2));
console.log(decimalPrecision(673.35259, 3));
300.36
673.353

Pagination: Total Pages required

const totalRecords = 243;
const perPage = 10;

const totalPages = Math.ceil(totalRecords / perPage);
console.log("Required Pages = ", totalPages);
Required Pages =  25

Calculate the time required to download

If we have a file size of 150042 MB and the internet download speed is 100 MB/min.

const fileSize = 150042;
const speed = 100;

const t = Math.ceil(fileSize / speed);
console.log("Time in Minutes = " + t);
Time in Minutes = 1501

JavaScript ceil() Error and Exceptions

When we pass an argument that can’t be implicitly converted to a numeric value, the ceil() function will return NaN. For instance, Hi cannot be converted to a number, so it returns NaN.

console.log(Math.ceil("Hi"));
NaN

If you pass a null value or an empty string, the ceil() function returns 0 as the output. When you pass an empty array, it returns 0.

const a = []
console.log(Math.ceil(a));

console.log(Math.ceil(" "));
console.log(Math.ceil(null));
0
0
0