라이브러리
[JAVASCRIPT] Math.floor(x) - 내림하여 정수 반환
Math.floor(x)란 무엇인가?
Math.floor(x)는 JavaScript에서 사용하는 수학 함수 중 하나입니다. 이 함수는 주어진 숫자 x의 가장 가까운 정수(소수점 이하를 버리는)를 반환합니다.
Math.floor(x) 예제
#hostingforum.kr
javascript
console.log(Math.floor(3.7)); // 출력: 3
console.log(Math.floor(-3.7)); // 출력: -4
console.log(Math.floor(5)); // 출력: 5
console.log(Math.floor(-5)); // 출력: -5
위 예제에서 Math.floor(x)를 사용하여 주어진 숫자의 가장 가까운 정수를 반환했습니다.
Math.floor(x) 사용법
Math.floor(x)를 사용하는 방법은 간단합니다. 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
javascript
let x = 3.7;
let result = Math.floor(x);
console.log(result); // 출력: 3
Math.floor(x)와 Math.ceil(x)의 차이
Math.floor(x)와 Math.ceil(x)는 둘 다 숫자의 가장 가까운 정수를 반환하지만, Math.ceil(x)는 소수점 이하를 올려주는 반면 Math.floor(x)는 소수점 이하를 버립니다.
#hostingforum.kr
javascript
console.log(Math.floor(3.7)); // 출력: 3
console.log(Math.ceil(3.7)); // 출력: 4
Math.floor(x)와 Math.round(x)의 차이
Math.floor(x)와 Math.round(x)는 둘 다 숫자의 가장 가까운 정수를 반환하지만, Math.round(x)는 소수점 이하가 0.5 이상이면 올려주고, 0.5 이하이면 버립니다.
#hostingforum.kr
javascript
console.log(Math.floor(3.7)); // 출력: 3
console.log(Math.round(3.7)); // 출력: 4
console.log(Math.floor(-3.7)); // 출력: -4
console.log(Math.round(-3.7)); // 출력: -4
위 예제에서 Math.floor(x)와 Math.round(x)의 차이를 확인할 수 있습니다.
댓글목록
등록된 댓글이 없습니다.