Getting started with JS math ceiling
The ceil()
method in JavaScript returns the smallest integer that is greater than or equal to the given number. This method is helpful for rounding numbers up. In simpler words, the ceil()
method is a math function that rounds a number up to the nearest integer. This can be useful when you need to work with whole numbers in your code. In this article, we'll show you how to use the ceil()
method in JavaScript.
Syntax of math.ceil()
Using the ceil property of the Math object, you can round up the value of the variable dist.
The ceil property has the following syntax:
Math.ceil(number)
This will round up any floating-point number to the nearest integer.
When should we use ceil() over round()?
There are functions like Math.ceil
(for “ceiling,” which rounds up to a whole number) and Math.round
(to the nearest whole number). We use math.ceil
to turn decimals into integers. There are different ways to turn decimals into integers. Sometimes you will want to round a number. This you can do with the round() method:
let x = 6.78;
let y = 5.34;
console.log("X:", x, "becomes", Math.round(x));
console.log("Y:", y, "becomes", Math.round(y));
This will log:
X: 6.78 becomes 7 Y: 5.34 becomes 5
As you can see it is using normal rounding here. It is also possible that you don't want to round down, but up. For example, if you need to calculate how many wood boards you need and you conclude that you need 1.1
, 1
is not going to be enough to do the job. You'll need 2
. In this case, you can use the ceil()
method (referring to ceiling):
console.log("X:", x, "becomes", Math.ceil(x));
console.log("Y:", y, "becomes", Math.ceil(y));
This will log:
X: 6.78 becomes 7
Y: 5.34 becomes 6
How to use ceil
with negative numbers?
The ceil()
method is always rounding up to the first integer it encounters. We have used this before when we were generating random numbers! Careful with negative numbers here, because -5
is higher than -6
. This is how it works, as you can see in this example:
let negativeX = -x;
let negativeY = -y;
console.log("negativeX:", negativeX, "becomes", Math.ceil(negativeX));
console.log("negativeY:", negativeY, "becomes", Math.ceil(negativeY));
This will log:
negativeX: -6.78 becomes -6 negativeY: -5.34 becomes -5
Round up using ceil
method in JavaScript
Mathematics allows us to round up or down numbers, but to round down numbers, there are different approaches to doing that. However, if we want to round up numbers, the only appropriate method is the ceil
method. We can use the round()
method, but it is only when the decimal point of the number is greater than or equal to 0.5, it rounds up to the next higher absolute value.
If we want to round the number regardless of whether the number is greater than 0.5 or not, we can use the ceil()
method. So, if the value is 4.01
, and we apply the ceil
method, it will return 5
. In summary, the ceil
method rounds up and returns the next integer higher than the number (higher absolute value).
Let’s see this action with some code.
number1 = 4.01;
number2 = 3.98;
number3 = 123.45;
console.log(Math.ceil(number1));
console.log(Math.ceil(number2));
console.log(Math.ceil(number3));
Output
5
4
124
As you can see regardless of how far (decimally) the number is from the higher (or next) absolute value, it will round up to it via the ceil()
method.
Summary
Numbers are interesting to work with in JavaScript, and rounding numbers can be done in different ways, and the ceil()
method via the Math
object. The typical rounding of numbers can either be up or down. However, if we want to round up, the ceil
method gives us the result we want.
References
Math.ceil() - JavaScript | MDN (mozilla.org)