[JavaScript] Math함수

hana jeong·2022년 11월 17일
0

프로그래머스를 풀면서 배우게 된 math 함수에 대해서 정리했다

  1. 올림(Math.ceil)
  2. 내림(Math.floor)
  3. 반올림(Math.round)
  4. 가장 큰 수(Math.max)
  5. 가장 작은 수(Math.min)
  6. 랜덤(Math.random)

1. 올림(Math.ceil)

인수로 받은 값과 같거나 큰 수 중에서 가장 작은 정수를 반환함
그러니까 올림한 정수를 리턴한다고 보면 됨

예를 들면

const test = Math.ceil(1) 
console.log(test) 
// 1 출력

const test2 = Math.ceil(2.35)
console.log(test2)
// 3 출력

const testt = Math.ceil(1.222 * 10) / 10;
console.log(testt)
// 1.3 출력

const testt = Math.ceil(1.222 * 100) / 100;
console.log(testt)
// 1.23 출력

const testt = Math.ceil(1222 * 10) / 10;
console.log(testt)
// 1230 출력

const testt = Math.ceil(1222 * 100) / 100;
console.log(testt)
// 1300 출력

const test3 = Math.ceil(-1)
console.log(test3)
// -1 출력

const test4 = Math.ceil(-2.5)
console.log(test4)
// -2 출력

0과 null도 0으로 리턴된다

const test5 = Math.ceil(0)
console.log(test5)
// 0 출력

const test6 = Math.ceil(null)
console.log(test6)
// 0 출력

2. 내림(Math.floor)

인수로 받은 값과 같거나 작은 수 중 가장 큰 정수 반환
즉 내림한 정수를 리턴한다는 뜻

const test = Math.floor(1) 
console.log(test) 
// 1 출력

const test2 = Math.floor(2.35)
console.log(test2)
// 2 출력

const test3 = Math.floor(-1)
console.log(test3)
// -1 출력

const test4 = Math.floor(-2.5)
console.log(test4)
// -3 출력

const testt = Math.floor(1.777 * 10) / 10;
console.log(testt)
// 1.7 출력

const testt = Math.floor(1.222 * 100) / 100;
console.log(testt)
// 1.77 출력

const testt = Math.floor(1777 * 10) / 10;
console.log(testt)
// 1770 출력

const testt = Math.floor(1777 * 100) / 100;
console.log(testt)
// 1700 출력

0과 null도 0으로 리턴된다

const test5 = Math.floor(0)
console.log(test5)
// 0 출력

const test6 = Math.floor(null)
console.log(test6)
// 0 출력

3. 반올림(Math.round)

인수로 받은 값을 소수점 첫 번째 자리에서 반올림해서 리턴함

const test = Math.round(1) 
console.log(test) 
// 1 출력

const test2 = Math.round(2.5)
console.log(test2)
// 2 출력

const test3 = Math.round(-1)
console.log(test3)
// -1 출력

const test4 = Math.round(-2.5)
console.log(test4)
// -2 출력

const test4 = Math.round(-2.7)
console.log(test4)
// -3 출력

const testt = Math.round(1.222 * 10) / 10;
console.log(testt)
// 1.2 출력

const testt = Math.round(1.5 * 10) / 10;
console.log(testt)
// 1.5 출력

const testt = Math.round(1777 * 10) / 10;
console.log(testt)
// 1.8 출력

4. 가장 큰 수(Math.max)

인수로 받은 값 중 가장 큰 수를 리턴함
인수가 전달되지 않으면 -infinity를 반환하고 인수 중 비교할 수 없는 값이 포함되어 있으면 NaN(Not a Number)리턴함

const test2 = Math.max()
console.log(test2)
// -infinity

const test2 = Math.max(1, 10, -2, 100);
console.log(test2)
// 100

const test2 = Math.max(1, 10, "1000", -2);
console.log(test2)
// 1000

const test2 = Math.max(1, 10, "string", -2);
console.log(test2)
// NaN

5. 가장 작은 수(Math.min)

인수로 받은 값 중 가장 작은 수를 리턴함
인수가 전달되지 않으면 -infinity를 반환하고 인수 중 비교할 수 없는 값이 포함되어 있으면 NaN(Not a Number)리턴함

const test2 = Math.max()
console.log(test2)
// -infinity

const test2 = Math.max(1, 10, -2, 100);
console.log(test2)
// 100

const test2 = Math.max(1, 10, "1000", -2);
console.log(test2)
// 1000

const test2 = Math.max(1, 10, "string", -2);
console.log(test2)
// NaN

6. 랜덤(Math.random)

말 그대로 랜덤으로 숫자를 리턴함
Math.random()의 경우 0보다 크거나 같고 1보다 작은 무작위 숫자를 리턴함

const min =10 max = 20;  
Math.random(); 
// [0,1]

Math.random() * (max - min) + min; 
//[min, max]

그리고 항상 Math를 쓸 때는 대문자 주의해서 쓰자!

출처: https://hianna.tistory.com/446
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math
http://www.tcpschool.com/javascript/js_standard_math

profile
https://developer-hh.tistory.com 로 옮깁니다

0개의 댓글