본문 바로가기
JavaScript

math function

by 죠스^ 2021. 3. 15.

math function

 ■ Math.ceil( ) → 소수자 이하 올림

 ■ Math.round( ) → 소수자 이하 반올림

 ■ Math.floor( ) → 소수자 이하 내림

 ■ Math.abs( ) → 절대 값(양수 값) 반환

 ■ Math.min( ) → 전달된 인자 중 작은 값 반환

 ■ Math.max( ) → 전달된 인자 중 큰 값 반환

 ■ Math.random( ) 0~1사이의 수를 무작위로 반환

 

 ■ Math.ceil( ) → 소수자 이하 올림

===============

<script>

document.write(Math.ceil(4.3));

</script>

===============

 

※ 출력

5

 

 ■ Math.round( ) → 소수자 이하 반올림

===============

<script>

document.write(Math.round(4.6));

document.write("<br>");

document.write(Math.round(4.4));

</script>

===============

 

※ 출력

5

4

 

 ■ Math.floor( ) → 소수자 이하 내림

===============

<script>

document.write(Math.floor(4.6));

document.write("<br>");

document.write(Math.floor(-4.4)); 

</script>

===============

 

※ 출력

4

-5

 

 ■ Math.abs( ) → 절대 값(양수 값) 반환

===============

<script>

document.write(Math.abs(5));

document.write("<br>");

document.write(Math.abs(-5)); 

</script>

===============

 

※ 출력

5

5

 

 ■ Math.min( ) → 전달된 인자 중 작은 값 반환

===============

<script>

document.write(Math.min(0, 150, 30, 20, -8, -200));

</script>

===============

 

※ 출력

-200

 

 ■ Math.max( ) → 전달된 인자 중 큰 값 반환

===============

<script>

document.write(Math.max(0, 150, 30, 20, -8, -200));

</script>

===============

 

※ 출력

150

 

 ■ Math.random( ) → 0과 1사이의 수를 무작위로 반환

===============

<script>

for(let i = 0 ; i <10 ; i++){

document.write(Math.random());

document.write("<br>");

}

</script>

===============

 

※ 출력

0.35716611055191083
0.9025054482760404
0.8721021875534014
0.18461836457518732
0.8892829509736995
0.8675868599409595
0.367258356750612
0.8490996776387212
0.10301764170642125
0.5002898290174083

 

사이의 값을 반환하는 함수

parseInt(Math.random( ) * ( - 작은 ) + 작은 수)      // 수 않나옴

parseInt(Math.random( ) * (( +1) - 작은 ) + 작은 수) //  나옴 

 

▶ 3과 7(6) 사이의 값을 무작위 반환

===============

<script>

      for (let i = 0; i < 10; i++{

        document.write(parseInt(Math.random() * (7 - 3+ 3)); // 3 ~ 6

        document.write(", ");

        document.write(parseInt(Math.random() * (8 - 3+ 3)); // 3 ~ 7

        document.write("<br>");

      }

</script>

===============

 

※ 출력

4, 5
4, 7
6, 4
6, 3
5, 3

 

'JavaScript' 카테고리의 다른 글

for 문 (for / for in / for of)  (0) 2021.03.15
array function  (0) 2021.03.15
string function  (0) 2021.03.15