[JavaScript] 올림/반올림/내림

태연·2025년 2월 6일
0

공부

목록 보기
3/4

Pagination 구현하려고 공부하다가 Math.ceil함수를 발견했다.
오랜만에 보니까 헷갈려서 정리를 해놔야겠다...

숫자를 올림, 반올림, 내림을 수행하는 함수는 각각 Math.ceil(x), Math.round(x), Math.floor(x)가 있다(여기서 x는 숫자).

Math.ceil 함수

소수값이 있을때 값을 올림

Math.ceil(0.8); //결과값: 1
Math.ceil(2); //결과값: 2
Math.ceil(3.0002); //결과값: 4
Math.ceil(-3.0002); //결과값: -3

Math.round 함수

소수값이 있을때 가장 가까운 정수로 반올림

Math.round(5.5); //결과값: 6
Math.round(5.05); //결과값: 5
Math.round(-5.05); //결과값: -5
Math.round(-5.5); //결과값: -5
 Math.round(-5.95); //결과값: -6

Math.floor 함수

소수값이 있을때 값을 내림

Math.round(5.94); //결과값: 5
Math.round(5.05); //결과값: 5
Math.round(-5.05); //결과값: -6
Math.round(5); //결과값: 5

profile
어서와

0개의 댓글