JS Math 메소드 (Math.max, Math.min, Math.random, Math.round, Math.floor, Math.ceil, Math.trunc)
2021. 7. 25. 00:10
728x90
01 Math.max() 메소드 / Math.min() 메소드
Math.max() 메소드는 인수 중에서 가장 큰 수를 반환.
인수가 전달되지 않으면 -Infinity를 반환하며, 비교할 수 없는 값이 포함되어 있으면 NaN을 반환.
Math.min() 메소드는 인수 중에서 가장 작은 수를 반환.
인수가 전달되지 않으면 Infinity를 반환하며, 비교할 수 없는 값이 포함되어 있으면 NaN을 반환.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var max = Math.max(21,324,23,55);
document.write(max,"<br>");
var min = Math.min(21,324,23,55);
document.write(min,"<br>");
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
Math.max(); // -Infinity
Math.max(21,324,23,55); // 324
Math.max(121,324,"23",55); // 324
Math.max(21,324,23,"문자열",55); // NaN
Math.min(); // Infinity
Math.min(21,324,23,55); // 23
Math.min(121,324,"23",55); // 23
Math.min(21,324,23,"문자열",55); // NaN
</script>
</head>
<body>
</body>
</html>
02 Math.random() 메소드
Math.random() 메소드는 0보다 크거나 같고 1보다 작은 소수점을 반환. 0은 포함 되지만 1은 포함되지 않는다.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var rand = Math.random();
document.write("rand : " + rand);
</script>
</head>
<body>
</body>
</html>
03 Math.round() 메소드
Math.round() 메소드는 인수의 소수점 이하를 반올림한 정수를 반환.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var n = -4.4567;
var round = Math.round(n); //반올림
document.write(round);
</script>
</head>
<body>
</body>
</html>
04 Math.floor() 메소드
Math.floor() 메소드는 인수의 소수점 이하를 내림한 정수를 반환.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var n = -4.4567;
var floor = Math.floor(n); //내림
document.write(floor);
</script>
</head>
<body>
</body>
</html>
05 Math.ceil() 메소드
Math.round() 메소드는 인수의 소수점 이하를 올림한 정수를 반환.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var n = -4.4567;
var ceil = Math.ceil(n); //올림
document.write(ceil);
</script>
</head>
<body>
</body>
</html>
06 Math.trunc() 메소드
Math.trunc() 메소드는 모든 소수 부분을 삭제하고 정수 부분만을 반환함.
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>math 메소드</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var n = -4.4567;
var trunc = Math.trunc(n); //소수자리수 제거
document.write(trunc);
</script>
</head>
<body>
</body>
</html>
728x90
'JavaScript > Study' 카테고리의 다른 글
JS String 객체/프로퍼티 ("", '', .length,) (0) | 2021.07.30 |
---|---|
JS Math 프로퍼티 (Math.PI) (0) | 2021.07.30 |
[JS] Date 메소드 (Date.now, Date.prototype getter, Date.prototype setter) (0) | 2021.07.24 |
[JS] Date 객체 (new Date) (0) | 2021.07.20 |
JS 조건문 (switch) (0) | 2021.07.16 |