Math.ceil() Method in JS – How to Round Up Numbers
Whenever you invoke Math.ceil(), the method rounds up its argument to the nearest integer.
note
Math.ceil()
is one of the built-in methods of the globalMath
object.- The global
Math
object is one of the standard built-in JavaScript objects.
Syntax of the Math.ceil()
Method
Math.ceil()
accepts only one argument. Here is the syntax:
Math.ceil(value);
The value
argument represents the number you wish to round up to the nearest integer.
note
- Suppose the
value
argument is already an integer. In that case,Math.ceil()
will return the same number. Math.ceil()
is equivalent to-Math.floor(-value)
.
Examples
Below are examples of the Math.ceil()
method.
How to round up 9
to the nearest integer
Math.ceil(9);
// The invocation above will return: 9
How to round up 9.999
to the nearest integer
Math.ceil(9.999);
// The invocation above will return: 10
How to round up -9.999
to the nearest integer
Math.ceil(-9.999);
// The invocation above will return: -9
The snippet above returned -9
because -9
is the integer directly above -9.999
on the number line. In other words, -9
is greater than -9.999
. But -10
is less than -9.999
.
How to round up 0.91
to the nearest integer
Math.ceil(0.91);
// The invocation above will return: 1
How to round up 0.01
to the nearest integer
Math.ceil(0.01);
// The invocation above will return: 1
How to round up 0
to the nearest integer
Math.ceil(0);
// The invocation above will return: 0
CodeSweetly TIP
- To round down a number, use
Math.floor()
. - To generate random numbers, use
Math.random()
.
Overview
This article discussed what JavaScript's Math.ceil()
method does. We also used examples to see how it works.