Skip to main content

floor()

floor(n, precision): number

Computes number rounded down to precision.

DEPRECATED

Use Math.floor() directly instead.


Parametersโ€‹

n: numberโ€‹

The number to round down.

precision: number = 0โ€‹

The precision to round down to. Defaults to 0.


Returns: numberโ€‹

The rounded down number.


See Alsoโ€‹

Math.floor() - MDN


Sinceโ€‹

2.0.0


Also known asโ€‹

floor (Lodash, es-toolkit) ยท โŒ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Exampleโ€‹

// โŒ Deprecated approach
floor(4.906); // => 4
floor(0.046, 2); // => 0.04
floor(4060, -2); // => 4000

// โœ… Recommended approach
Math.floor(4.906); // => 4
Math.floor(0.046 * 100) / 100; // => 0.04
Math.floor(4060 / 100) * 100; // => 4000

How it works?โ€‹

Rounds number down to specified precision. Deprecated: Use Math.floor() directly.

Native Equivalentโ€‹

// โŒ floor(value)
// โœ… Math.floor(value)

Use Casesโ€‹

Round down ๐Ÿ“Œโ€‹

Round number down to nearest integer.

Math.floor(4.9);   // 4
Math.floor(-4.1); // -5

Calculate indexโ€‹

Calculate array index from ratio.

const index = Math.floor(Math.random() * items.length);