Baseline Widely available
Math.floor()
ã¯éçã¡ã½ããã§ãä¸ããããæ°å¤ä»¥ä¸ã®æå¤§ã®æ´æ°ãè¿ãã¾ãã
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
æ§æ 弿° è¿å¤
x
以ä¸ã®æå¤§ã®æ´æ°ã§ãããã㯠-Math.ceil(-x)
ã¨åãå¤ã§ãã
floor()
㯠Math
ãªãã¸ã§ã¯ãã®éçãªã¡ã½ãããªã®ã§ãèªãçæãã Math
ãªãã¸ã§ã¯ãã®ã¡ã½ããã¨ãã¦ã§ã¯ãªãã常ã«ãMath.floor()
ã¨ãã¦ä½¿ç¨ããããã«ãã¦ãã ãã (Math
ã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã¯ããã¾ãã)ã
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
å鲿°ã®ä¸¸ã
ãã®ä¾ã§ã¯ã decimalAdjust()
ã¨ããã¡ã½ãããå®è£
ãã¾ããããã¯ãMath.floor()
ãMath.ceil()
ãMath.round()
ã®æ¡å¼µã¡ã½ããã§ãã Math
ã® 3 ã¤ã®é¢æ°ã¯å¸¸ã«æ°å¤ãå°æ°ç¹ä»¥ä¸ã®æ¡æ°ã«èª¿æ´ãã¾ããã decimalAdjust
㯠exp
弿°ãåãå
¥ããæ°å¤ã調æ´ããå°æ°ç¹ä»¥ä¸ã®æ¡æ°ãæå®ãã¾ããä¾ãã°ã -1
ã¯å°æ°ç¹ä»¥ä¸ 1 æ¡ï¼"à 10-1" ã®ããã«ï¼ãæå³ãã¾ããããã«ãround
ãfloor
ãceil
ã®ããããã®èª¿æ´æ¹æ³ã type
弿°ã«ãã鏿ã§ãã¾ãã
ããã¯ãæ°å¤ã« 10 ã®ç´¯ä¹ãä¹ç®ãããã®çµæãæãè¿ãæ´æ°ã«ä¸¸ããããã« 10 ã®ç´¯ä¹ã§å²ããã¨ã§è¡ãã¾ãããã精度ãç¶æããããã«ãNumber ã® toString()
ã¡ã½ãããå©ç¨ãã¾ãããã®ã¡ã½ããã¯ã大ããªæ°å¤ãå°ããªæ°å¤ãç§å¦è¨æ³ï¼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
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4