This page was translated from English by the community.Learn more and join the MDN Web Docs community.
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() 정적 메서드는 언제나 버림 처리하고 주어진 숫자와 같거나 작은 정수 중에서 가장 큰 수를 반환합니다.
In this article
시도해 보기
console.log(Math.floor(5.95));// Expected output: 5console.log(Math.floor(5.05));// Expected output: 5console.log(Math.floor(5));// Expected output: 5console.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); // -InfinityMath.floor(-45.95); // -46Math.floor(-45.05); // -46Math.floor(-0); // -0Math.floor(0); // 0Math.floor(4); // 4Math.floor(45.05); // 45Math.floor(45.95); // 45Math.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.6round10(55.549, -1); // 55.5round10(55, 1); // 60round10(54.9, 1); // 50round10(-55.55, -1); // -55.5round10(-55.551, -1); // -55.6round10(-55, 1); // -50round10(-55.1, 1); // -60// 버립floor10(55.59, -1); // 55.5floor10(59, 1); // 50floor10(-55.51, -1); // -55.6floor10(-51, 1); // -60// 올림ceil10(55.51, -1); // 55.6ceil10(51, 1); // 60ceil10(-55.59, -1); // -55.5ceil10(-59, 1); // -50명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.floor> |