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.
Die statische Methode Math.floor()
rundet immer ab und gibt die größte ganze Zahl zurück, die kleiner oder gleich einer gegebenen Zahl ist.
Probieren Sie es aus
console.log(Math.floor(5.95));
// Expected output: 5
console.log(Math.floor(5.05));
// Expected output: 5
console.log(Math.floor(5));
// Expected output: 5
console.log(Math.floor(-5.05));
// Expected output: -6
Syntax
Math.floor(x)
Parameter
x
-
Eine Zahl.
Rückgabewert
Die größte ganze Zahl, die kleiner oder gleich x
ist. Sie entspricht dem Wert von -Math.ceil(-x)
.
Beschreibung
Da floor()
eine statische Methode von Math
ist, wird sie immer als Math.floor()
verwendet, und nicht als Methode eines erstellten Math
-Objekts (Math
ist kein Konstruktor).
Beispiele
Verwendung von Math.floor()
Math.floor(-Infinity); // -Infinity
Math.floor(-45.95); // -46
Math.floor(-45.05); // -46
Math.floor(-0); // -0
Math.floor(0); // 0
Math.floor(4); // 4
Math.floor(45.05); // 45
Math.floor(45.95); // 45
Math.floor(Infinity); // Infinity
Dezimalanpassung
In diesem Beispiel implementieren wir eine Methode namens decimalAdjust()
, die eine Erweiterungsmethode von Math.floor()
, Math.ceil()
und Math.round()
ist. Während die drei Math
-Funktionen den Eingabewert immer auf die Einerstelle anpassen, akzeptiert decimalAdjust
einen exp
-Parameter, der die Anzahl der Stellen links vom Dezimalkomma angibt, auf die die Zahl angepasst werden soll. Zum Beispiel bedeutet -1
, dass es eine Stelle nach dem Dezimalkomma belässt (wie in "× 10-1"). Darüber hinaus erlaubt es Ihnen, das Mittel der Anpassung auszuwählen — round
, floor
oder ceil
— durch den type
-Parameter.
Dies geschieht, indem die Zahl mit einer Zehnerpotenz multipliziert wird, dann das Ergebnis auf die nächste Ganzzahl gerundet und anschließend durch die Zehnerpotenz dividiert wird. Um die Genauigkeit besser zu erhalten, wird die Methode toString()
von Number genutzt, die große oder kleine Zahlen in wissenschaftlicher Notation darstellt (wie 6.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 round
const round10 = (value, exp) => decimalAdjust("round", value, exp);
// Decimal floor
const floor10 = (value, exp) => decimalAdjust("floor", value, exp);
// Decimal ceil
const ceil10 = (value, exp) => decimalAdjust("ceil", value, exp);
// Round
round10(55.55, -1); // 55.6
round10(55.549, -1); // 55.5
round10(55, 1); // 60
round10(54.9, 1); // 50
round10(-55.55, -1); // -55.5
round10(-55.551, -1); // -55.6
round10(-55, 1); // -50
round10(-55.1, 1); // -60
// Floor
floor10(55.59, -1); // 55.5
floor10(59, 1); // 50
floor10(-55.51, -1); // -55.6
floor10(-51, 1); // -60
// Ceil
ceil10(55.51, -1); // 55.6
ceil10(51, 1); // 60
ceil10(-55.59, -1); // -55.5
ceil10(-59, 1); // -50
Spezifikationen
Specification |
---|
ECMAScript® 2026 Language Specification # sec-math.floor |