Math.floor()
Math.floor()
静态方法始终向下舍入并返回小于或等于给定数字的最大整数。
¥The Math.floor()
static method always rounds down and returns the largest integer less than or equal to a given number.
Try it
语法
参数
返回值
¥Return value
小于或等于 x
的最大整数。它与 -Math.ceil(-x)
的值相同。
¥The largest integer smaller than or equal to x
. It's the same value as -Math.ceil(-x)
.
描述
示例
使用 Math.floor()
小数调整
¥Decimal adjustment
在这个例子中,我们实现了一个名为 decimalAdjust()
的方法,它是 Math.floor()
、Math.ceil()
和 Math.round()
的增强方法。虽然三个 Math
函数始终将输入调整为个位数,但 decimalAdjust
接受 exp
参数,该参数指定小数点左侧的数字应调整到的位数。例如,-1
表示小数点后保留一位数字(如 "× 10-1")。此外,它还允许你通过 type
参数选择调整方式 - round
、floor
或 ceil
。
¥In this example, we implement a method called decimalAdjust()
that is an enhancement method of Math.floor()
, Math.ceil()
, and Math.round()
. While the three Math
functions always adjust the input to the units digit, decimalAdjust
accepts an exp
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
, or ceil
— through the type
parameter.
它通过将数字乘以 10 的幂,然后将结果四舍五入到最接近的整数,然后除以 10 的幂来实现。为了更好地保持精度,它利用了 Number 的 toString()
方法,该方法以科学记数法表示大数或小数(如 6.02e23
)。
¥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's toString()
method, which represents large or small numbers in scientific notation (like 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${+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
规范
Specification |
---|
ECMAScript Language Specification # sec-math.floor |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also