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 โจ2015๋ 7์โฉ.
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
๊ตฌ๋ฌธ
Math.floor(x)
๋งค๊ฐ๋ณ์
x
-
์ซ์.
๋ฐํ ๊ฐ
x
์ ๊ฐ๊ฑฐ๋ ์์ ์ ์ ์ค ๊ฐ์ฅ ํฐ ์. -Math.ceil(-x)
์ ๊ฐ์ ๊ฐ์
๋๋ค.
์ค๋ช
floor()
๋ Math
์ ์ ์ ๋ฉ์๋์ด๋ฏ๋ก, ์์ฑํ Math
๊ฐ์ฒด(Math
๋ ์์ฑ์๊ฐ ์๋๋๋ค)์ ๋ฉ์๋ ๋ณด๋ค๋ ํญ์ Math.floor()
๋ฅผ ์ฌ์ฉํ์ธ์.
์์
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
๋ช ์ธ์
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-math.floor |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ