Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Math
  6. floor()

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.

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: -6

Syntax

js
Math.floor(x)

Parameters

x

A 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()

js
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); // Infinity

Decimal 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).

js
/** * 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); // -50

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-math.floor

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp