본문 바로가기
Language/JavaScript

[JavaScript] Math.(round/ceil/floor) - 반올림/올림/내림

by 썸머워즈 2020. 12. 15.
반응형

크립트에는 수학에서 자주 사용하는 상수와 함수들을 미리 구현해 놓은 내장 객체인 Math 객체가 존재한다.

 

Math 객체 안에는 아주 다양한 메서드가 존재하는데 그 중에서

우리는 흔히 쓰이는 반올림(round)/올림(ceil)/내림(floor) 메서드에 대해 알아보고자 한다. 

 

▷ 구문

Math.round(num) : 반올림
Math.ceil(num) : 올림
Math.floor(num) : 내림

 

예제를 통해 알아보자.

 


▷ 예제1) Math.round()

Math.round(7.5); //8
Math.round(7.4); //7
Math.round(7.48); //7

Math.round(-7.5); //-7
Math.round(-7.6); //-8

 

▷ 예제2) Math.ceil()

Math.ceil(6.4) //7
Math.ceil(6.01) //7
Math.ceil(6.000001) //7
Math.ceil(6.0) //6

Math.ceil(-6.8) //-6
Math.ceil(-6.0008) //-6

 

▷ 예제3) Math.floor()

Math.floor(5.9) //5
Math.floor(5.01) //5
Math.floor(5.000001) //5

Math.floor(-5.9) //-6
Math.floor(-5.0008) //-6
반응형


댓글

TOP