1.반올림,올림,내림
반올림 함수
console.log(Math.round(2.5));
console.log(Math.round(2.49));
console.log(Math.round(2));
console.log(Math.round(2.82));
- 반올림 함수는 평점,평균을 계산하고 별점을 매길 경우 등 사용자들이 좀 더 보기 쉽게 표현하기 위해 사용한다.
올림 함수
console.log(Math.ceil(2.5));
console.log(Math.ceil(2.49));
console.log(Math.ceil(2));
console.log(Math.ceil(2.82));
내림 함수
console.log(Math.floor(2.5));
console.log(Math.floor(2.49));
console.log(Math.floor(2));
console.log(Math.floor(2.82));
2.랜덤 함수
- 랜덤 함수는 JavaScript에 이미 구현되어있다.
var randomNumber = Math.random();
console.log(randomNumber);
- 랜덤 함수는 0.0000000000000000에서 0.9999999999999999 사이의 값에서 랜덤수를 제공하지만,이 랜덤함수를 이용해서 개발자가 원하는 범위의 랜덤수를 설정할 수 있다.
var randomNumber = Math.random();
console.log(Math.floor(randomNumber*10));