Math.floor()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheMath.floor() static method always rounds down and returns the largest integer less than or equal to a given number.
In this article
Try it
console.log(Math.floor(5.95));// Expected output: 5console.log(Math.floor(5.05));// Expected output: 5console.log(Math.floor(5));// Expected output: 5console.log(Math.floor(-5.05));// Expected output: -6Syntax
Math.floor(x)Parameters
xA number.
Return value
The largest integer smaller than or equal tox. It's the same value as-Math.ceil(-x).
Description
Becausefloor() is a static method ofMath, you always use it asMath.floor(), rather than as a method of aMath object you created (Math is not a constructor).
Examples
>Using Math.floor()
Math.floor(-Infinity); // -InfinityMath.floor(-45.95); // -46Math.floor(-45.05); // -46Math.floor(-0); // -0Math.floor(0); // 0Math.floor(4); // 4Math.floor(45.05); // 45Math.floor(45.95); // 45Math.floor(Infinity); // InfinityDecimal adjustment
In this example, we implement a method calleddecimalAdjust() that is an enhancement method ofMath.floor(),Math.ceil(), andMath.round(). While the threeMath functions always adjust the input to the units digit,decimalAdjust accepts anexp parameter that specifies the number of digits to the left of the decimal point to which the number should be adjusted. For example,-1 means it would leave one digit after the decimal point (as in "× 10-1"). In addition, it allows you to select the means of adjustment —round,floor, orceil — through thetype parameter.
It does so by multiplying the number by a power of 10, then rounding the result to the nearest integer, then dividing by the power of 10. To better preserve precision, it takes advantage of Number'stoString() method, which represents large or small numbers in scientific notation (like6.02e23).
/** * Adjusts a number to the specified digit. * * @param {"round" | "floor" | "ceil"} type The type of adjustment. * @param {number} value The number. * @param {number} exp The exponent (the 10 logarithm of the adjustment base). * @returns {number} The adjusted value. */function decimalAdjust(type, value, exp) { type = String(type); if (!["round", "floor", "ceil"].includes(type)) { throw new TypeError( "The type of decimal adjustment must be one of 'round', 'floor', or 'ceil'.", ); } exp = Number(exp); value = Number(value); if (exp % 1 !== 0 || Number.isNaN(value)) { return NaN; } else if (exp === 0) { return Math[type](value); } const [magnitude, exponent = 0] = value.toString().split("e"); const adjustedValue = Math[type](`${magnitude}e${exponent - exp}`); // Shift back const [newMagnitude, newExponent = 0] = adjustedValue.toString().split("e"); return Number(`${newMagnitude}e${Number(newExponent) + exp}`);}// Decimal roundconst round10 = (value, exp) => decimalAdjust("round", value, exp);// Decimal floorconst floor10 = (value, exp) => decimalAdjust("floor", value, exp);// Decimal ceilconst ceil10 = (value, exp) => decimalAdjust("ceil", value, exp);// Roundround10(55.55, -1); // 55.6round10(55.549, -1); // 55.5round10(55, 1); // 60round10(54.9, 1); // 50round10(-55.55, -1); // -55.5round10(-55.551, -1); // -55.6round10(-55, 1); // -50round10(-55.1, 1); // -60// Floorfloor10(55.59, -1); // 55.5floor10(59, 1); // 50floor10(-55.51, -1); // -55.6floor10(-51, 1); // -60// Ceilceil10(55.51, -1); // 55.6ceil10(51, 1); // 60ceil10(-55.59, -1); // -55.5ceil10(-59, 1); // -50Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.floor> |