JS 노트(Math.ceil / Math.floor / Math.round)

주재일·2021년 5월 15일

참고사이트
https://m.blog.naver.com/PostView.nhn?blogId=diceworld&logNo=220174711730&proxyReferer=https:%2F%2Fwww.google.com%2F

const num = ["7", "2"];

let a = num[0]%num[1]; // 나머지 값 1
let b = Math.floor(parseInt(num[0])/parseInt(num[1])); //3.5 소수점 버리기 

console.log(a, b);

b 연산 시 7/2는 3.5라는 값이 출력되는데
Math.Floor 를 이용하면 소수점 이하 자리를 버리기 때문에

3을 출력합니다.

그 외 알아두면 좋은 메서드

   Math.ceil 은 소수값이 존재할 때 값을 올리는 역활을 하는 함수이며,

   Math.round 는 소수값에 따라 올리거나 버리는 역활을 하는 반올림 함수입니다.


profile
늦게 시작했으니 저는 늦둥이인가요?

0개의 댓글