본문 바로가기

02. JavaScript/01. 기본개념, 문법 (ES6)

JavaScript_03. 내장 객체_Math

※ 본 내용은 해당 교재를 참고하여 공부한 내용과 본인의 생각을 정리한 글입니다.

https://www.yes24.com/Product/Goods/105608999

 

바닐라 자바스크립트 - 예스24

실무 역량까지 한 번에 잡을 수 있는바닐라 자바스크립트 이론서자바스크립트는 이제 브라우저를 넘어 웹 애플리케이션뿐만 아니라 마이크로 컨트롤러까지 점점 사용하는 곳이 늘어나고 있다.

www.yes24.com

 

 

  Math 객체는 수학적인 상수와 내장 함수를 가진 객체이다. 다른 객체와 달리 Math는 생성자가 아니다. Math는 숫자 자료형만 지원한다.

 

● Math.round()

숫자에 대한 반올림 처리를 한다.

 

● Math.ceil()

숫자에 대한 무조건 올림 처리를 한다.

 

● Math.floor()

숫자에 대한 무조건 내림처리를 한다.

 

● Math.trunc()

소수 부분은 무조건 버리고 정수 부분만 반환한다.

 

● Math.sign()

숫자가 양수인지 음수인지 확인하는 용도로 사용된다. 양수이면 1, 음수이면 -1, 0이면 반환한다.

 

● Math.pow()

제곱근 값을 반환한다.

 

● Math.sqrt()

루트 값을 반환한다.

 

● Math.abs()

값을 무조건 양수로 반환한다.

 

● Math.min(), Math.max()

가장 작은 값을 반환하고, 가장 큰 값을 반환한다.

 

● Math.random()

0보다 크고 1보다 작은 숫자형 값을 반환한다.

 

<!DOCTYPE html>
<html>

<head>
    <title>Document</title>
</head>

<body>
    <script>
        console.log("Math.round(4.9)  = " + Math.round(4.9)); // returns 5
        console.log("Math.round(4.7)  = " + Math.round(4.7)); // returns 5
        console.log("Math.round(4.4)  = " + Math.round(4.4)); // returns 4
        console.log("Math.round(4.2)  = " + Math.round(4.2)); // returns 4
        console.log("Math.round(-4.2)  = " + Math.round(-4.2)); // returns -4

        console.log("Math.ceil(4.9)  = " + Math.ceil(4.9)); // returns 5
        console.log("Math.ceil(4.7)  = " + Math.ceil(4.7)); // returns 5
        console.log("Math.ceil(4.4)  = " + Math.ceil(4.4)); // returns 5
        console.log("Math.ceil(4.2)  = " + Math.ceil(4.2)); // returns 5
        console.log("Math.ceil(-4.2)  = " + Math.ceil(-4.2)); // returns -4

        console.log("Math.floor(4.9)  = " + Math.floor(4.9)); // returns 4
        console.log("Math.floor(4.7)  = " + Math.floor(4.7)); // returns 4
        console.log("Math.floor(4.4)  = " + Math.floor(4.4)); // returns 4
        console.log("Math.floor(4.2)  = " + Math.floor(4.2)); // returns 4
        console.log("Math.floor(-4.2)  = " + Math.floor(-4.2)); // returns -5

        console.log("Math.trunc(4.9)  = " + Math.trunc(4.9)); // returns 4
        console.log("Math.trunc(4.7)  = " + Math.trunc(4.7)); // returns 4
        console.log("Math.trunc(4.4)  = " + Math.trunc(4.4)); // returns 4
        console.log("Math.trunc(4.2)  = " + Math.trunc(4.2)); // returns 4
        console.log("Math.trunc(-4.2)  = " + Math.trunc(-4.2)); // returns -4

        console.log("Math.sign(-4)  = " + Math.sign(-4)); // returns -1
        console.log("Math.sign(0  = " + Math.sign(0)); // returns 0
        console.log("Math.sign(4)  = " + Math.sign(4)); // returns 1

        console.log("Math.pow(8, 2) = " + Math.pow(8, 2)); // returns 64

        console.log("Math.sqrt(64) = " + Math.sqrt(64)); // returns 8

        console.log("Math.abs(-4.7) = " + Math.abs(-4.7)); // returns 4.7

        console.log("Math.min(0, 150, 30, 20, -8, -200) = " + Math.min(0, 150, 30, 20, -8, -200)); // returns -200
        console.log("Math.max(0, 150, 30, 20, -8, -200) = " + Math.max(0, 150, 30, 20, -8, -200)); // returns 150

        console.log("Math.random() = " + Math.random()); // returns a random number

        console.log("Math.floor(Math.random() * 10) = " + Math.floor(Math.random() * 10)); // 0에서 9사이의 정수

        console.log("Math.floor(Math.random() * 101) = " + Math.floor(Math.random() * 101)); // 0에서 100사이의 정수

        console.log("Math.floor(Math.random() * 10) + 1 = " + Math.floor(Math.random() * 10) + 1); // 1에서 10사이의 정수

        //최솟값과 최대값 사이의 랜덤한 정수를 반환하는 함수
        let result = function getRndInteger(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }

        console.log(result(1, 10000));
    </script>
</body>

</html>

 

console