This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

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

๊ตฌ๋ฌธ

js
Math.floor(x)

๋งค๊ฐœ๋ณ€์ˆ˜

x

์ˆซ์ž.

๋ฐ˜ํ™˜ ๊ฐ’

x์™€ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ์ •์ˆ˜ ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜. -Math.ceil(-x)์™€ ๊ฐ™์€ ๊ฐ’์ž…๋‹ˆ๋‹ค.

์„ค๋ช…

floor()๋Š” Math์˜ ์ •์  ๋ฉ”์„œ๋“œ์ด๋ฏ€๋กœ, ์ƒ์„ฑํ•œ Math ๊ฐ์ฒด(Math๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค)์˜ ๋ฉ”์„œ๋“œ ๋ณด๋‹ค๋Š” ํ•ญ์ƒ Math.floor()๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.

์˜ˆ์ œ

Math.floor() ์‚ฌ์šฉํ•˜๊ธฐ

js
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)์œผ๋กœ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

js
/**
 * ๋ช…์‹œ๋œ ์ž๋ฆฌ์ˆ˜์˜ ์ˆซ์ž ์กฐ์ •ํ•˜๊ธฐ
 *
 * @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

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ