어림 계산(반올림, 올림, 버림)
소수점을 버리거나 화면 크기를 정수로 조절할 때 사용합니다.
구문
Math.round(값): 반올림Math.floor(값): 버림(값보다 작고 가장 가까운 정수 반환)Math.ceil(값): 올림(값보다 크고 가장 가까운 정수 반환)Math.trunc(값): 값의 정수 부분만 반환
예시
Math.round(4.35); // 4
Math.floor(4.35); // 4
Math.ceil(4.35); // 5
Math.trunc(4.35); // 4
Math.round(4.61); // 5
Math.floor(4.61); // 4
Math.ceil(4.61); // 5
Math.trunc(4.61); // 4
Math.round(-4.49); // -4
Math.round(-4.5); // -4
Math.round(-4.51); // -5
Math.floor(-4.6); // -5
Math.ceil(-4.6); // -4
Math.trunc(-4.6); // -4