JavaScript Arithmetic (Math) Ceiling and Floor


Example

ceil()

The ceil() method rounds a number upwards to the nearest integer, and returns the result.

Syntax:

Math.ceil(n);

Example:

console.log(Math.ceil(0.60)); //  1
console.log(Math.ceil(0.40)); //  1
console.log(Math.ceil(5.1));  //  6
console.log(Math.ceil(-5.1)); // -5
console.log(Math.ceil(-5.9)); // -5

floor()

The floor() method rounds a number downwards to the nearest integer, and returns the result.

Syntax:

Math.floor(n);

Example:

console.log(Math.ceil(0.60)); //  0
console.log(Math.ceil(0.40)); //  0
console.log(Math.ceil(5.1));  //  5
console.log(Math.ceil(-5.1)); // -6
console.log(Math.ceil(-5.9)); // -6