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
ë ìì±ìê° ìëëë¤)ì ë©ìë ë³´ë¤ë íì 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
ìì§ì ì¡°ì
ì´ ìììì, ì°ë¦¬ë Math.floor()
, Math.ceil()
, ê·¸ë¦¬ê³ Math.round()
를 íì¥í ë©ìëì¸ decimalAdjust()
를 구íí©ëë¤. ì¸ ê°ì§ Math
í¨ìê° íì ì
ë ¥ì ì ì ë¨ìë¡ ì¡°ì íë ë°ë©´, decimalAdjust
ë ì«ì를 ì¡°ì í´ì¼ íë ììì ì¼ìª½ì ì릿ì를 ì§ì íë exp
매ê°ë³ì를 ë°ìµëë¤. ì를 ë¤ì´, -1
ì ììì ì´í í ì리를 ë¨ê¸´ë¤ë ì미ì
ëë¤ ("à 10-1"ì ê°ì´). ëí, type
매ê°ë³ì를 íµí´ ì¡°ì ë°©ì - round
, floor
, ëë ceil
- ì ì íí ì ììµëë¤.
ì´ë ì«ìì 10ì ê±°ëì ê³±ì ê³±í ë¤ì, 결과를 ê°ì¥ ê°ê¹ì´ ì ìë¡ ë°ì¬ë¦¼íê³ , ê·¸ ë¤ì 10ì ê±°ëì ê³±ì¼ë¡ ëëë ë°©ìì¼ë¡ ìëí©ëë¤. ì ë°ë를 ë ì ì ì§í기 ìí´, ì´ ë°©ë²ì Numberì toString()
ë©ìë를 íì©í©ëë¤. ì´ ë©ìëë í° ì«ìë ìì ì«ì를 ê³¼íì í기ë²(ì: 6.02e23
)ì¼ë¡ ííí©ëë¤.
/**
* ëª
ìë ì리ìì ì«ì ì¡°ì í기
*
* @param {"round" | "floor" | "ceil"} type ì¡°ì ì ì í.
* @param {number} value ì«ì ê°.
* @param {number} exp ì§ì(ì¡°ì 기ì¤ì 10 ë¡ê·¸)ì
ëë¤.
* @returns {number} ì¡°ì ë ê°.
*/
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}`);
// ë¤ë¡ ì´ë
const [newMagnitude, newExponent = 0] = adjustedValue.toString().split("e");
return Number(`${newMagnitude}e${+newExponent + exp}`);
}
// ìì§ë² ë°ì¬ë¦¼
const round10 = (value, exp) => decimalAdjust("round", value, exp);
// ìì§ë² ë²ë¦¼
const floor10 = (value, exp) => decimalAdjust("floor", value, exp);
// ìì§ë² ì¬ë¦¼
const ceil10 = (value, exp) => decimalAdjust("ceil", value, exp);
// ë°ì¬ë¦¼
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
// ë²ë¦½
floor10(55.59, -1); // 55.5
floor10(59, 1); // 50
floor10(-55.51, -1); // -55.6
floor10(-51, 1); // -60
// ì¬ë¦¼
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