상추의 IT저장소
JS) 자주쓰는 Math 메소드 본문
Math.max()
인수로 전달 받은 값 중에서 가장 큰 수를 반환한다.
예시>>
//Math.max
let max_a = Math.max();
let max_b = Math.max(1, 10, -100, -10, "-1000", 0);
let max_c = Math.max(1, 10, -100, -10, "문자열", 0);
console.log(max_a);
console.log(max_b);
console.log(max_c);
출력값>>

Math.min()
인수로 전달 받은 값 중에서 가장 작은 수를 반환한다.
예시>>
//Math.min
let min_a = Math.min();
let min_b = Math.min(1, 10, -100, -10, "-1000", 0);
let min_c = Math.min(1, 10 ,-100, -10, "문자열", 0);
console.log(min_a);
console.log(min_b);
console.log(min_c);
출력값>>

Math.random()
0보다 크거나 같고 1보다 작은 무작위 숫자를 반환한다.
예시>>
//Math.random
let min = 10;
let max = 20;
let r1 = Math.random();
let r2 = Math.random() * (max- min)+ min;
console.log(r1);
console.log(r2);
출력값>>

Math.round()
인수로 전달받은 값을 소수점 첫 번째 자리에서 반올림하여 값을 반환한다.
예시>>
//Math.round()
let round1 = Math.round(10.49);
let round2 = Math.round(11.01);
let round3 = Math.round(-10.95);
let round4 = Math.round(-11.01);
console.log("round1 : ",round1);
console.log("round2 : ",round2);
console.log("round3 : ",round3);
console.log("round4 : ",round4);
출력값>>

Math.floor()
인수로 전달받은 값과 같거나 작은 수 중에서 가장 큰 정수를 반환한다.(내림)
예시>>
//Math.floor()
let floor1 = Math.floor(10.95);
let floor2 = Math.floor(11.01);
let floor3 = Math.floor(-10.95);
let floor4 = Math.floor(-11.01);
console.log("floor1 : ",floor1);
console.log("floor2 : ",floor2);
console.log("floor3 : ",floor3);
console.log("floor4 : ",floor4);
출력값 >>

Math.ceil()
인수로 전달받은 값과 같거나 큰 수 중에서 가장 작은 정수를 반환한다.(올림)
예시>>
//Math.ceil()
let ceil1 = Math.ceil(10.49);
let ceil2 = Math.ceil(11.01);
let ceil3 = Math.ceil(-10.95);
let ceil4 = Math.ceil(-11.01);
console.log("ceil1 : ",ceil1);
console.log("ceil2 : ",ceil2);
console.log("ceil3 : ",ceil3);
console.log("ceil4 : ",ceil4);
출력값 >>

출처 : https://www.devkuma.com/docs/javascript/math-%EA%B0%9D%EC%B2%B4/
'Javascript' 카테고리의 다른 글
| JS) Promise, async/await (0) | 2022.11.20 |
|---|---|
| JS)Array.filter() Array.reduce() (0) | 2022.11.18 |
| JS) Array.map() (0) | 2022.11.15 |
| JS) 전역객체와 래퍼객체 (0) | 2022.11.15 |
| JS) 객체(object) (0) | 2022.11.13 |