IT 공부/JavaScript

Math객체

toraa 2022. 6. 8. 17:50

Math객체 활용
. 구현 : Math.메소드() (o)
            new Math(); (x)  

. 메소드
Math.abs() : 절대값
Math.ceil() : 올림
Math.floor() : 내림
Math.round() : 반올림
Math.pow(3,4) : 3의 4승
Math.random() : 0.1 ~ 1 미만의 임의의 수를 반환


<script type="text/javascript">

 var exp = new Array();
 exp[0] = Math.abs(-2.1);
 exp[1] = Math.ceil(3.45);
 exp[2] = Math.floor(3.45);
 exp[3] = Math.round(3.45);
 exp[4] = Math.max(5,7);
 exp[5] = Math.min(5,7);
 exp[6] = Math.pow(3,4);
 exp[7] = Math.PI; //파이

for ( i=0;  i<exp.length; i++ ){
document.write("<br>exp[" + i + "]= " + exp[i] );
}

</script>


<script type="text/javascript">

 var ran = Math.random();

 document.write(ran+"<br>");
 document.write((ran*10)+"<br>");
 document.write((ran*100)+"<br>");
 document.write(Math.floor((ran*10)+1)+"<br><br>");  1부터 10까지 랜덤숫자


 document.write("이번주 로또번호는 ");


 for ( i=0; i<6; i++ ){
 document.write((Math.floor(Math.random()*46)+1) + " ");
 }

</script>

'IT 공부 > JavaScript' 카테고리의 다른 글

Object  (0) 2022.06.09
Array  (1) 2022.06.09
String  (0) 2022.06.08
Date 객체  (0) 2022.06.08
실습  (0) 2022.06.07