Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.floor

Rounds a number down to an integer.

Available since: ES1

Syntax

Math.floor(x)

Parameters

ParameterTypeDescription
xnumberThe number to round.

Return value

numberThe largest integer not greater than `x`.

Examples

Rounding down, including negative numbers:

console.log(Math.floor(4.7));
console.log(Math.floor(-4.7));

Output:

4
-5

Math.ceil() rounds up, and Math.trunc() drops the fraction:

console.log(Math.ceil(4.2));
console.log(Math.trunc(-4.7));

Output:

5
-4

Notes

For negative numbers "down" means further from zero: Math.floor(-4.7) gives -5.

See also

  • Math.round — Rounds a number to the nearest integer.
  • Math.random — Returns a random number between 0 and 1.
  • Math.ceil — Rounds a number up to an integer.
  • Math.trunc — Drops the fractional part of a number.

Related recipes

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.