자바스크립트 Math개체

자바스크립트 Math개체

Javascript Math 개체를 사용하면 숫자에 대한 수학적 작업을 수행할 수 있습니다.

 

Math.PI;
//return 3.141592653589793

 

Math.min( )

가장 작은 수를 반환합니다.

Math.min(1, 10, -10, -100, -50) //-100

 

Math.max( )

가장 큰 수를 반환합니다.

Math.max(1, 10, -10, -100, -50) //10

 

 

Math.round( )

가장 가까운 정수로 값을 반환합니다.

Math.round(4.7); //5
Math.round(4.4); //4

 

Math.abs( )

절대(양수) 값을 반환합니다.

Math.abs(-4.7);      //4.7

 

Math.ceil( )

정수로 값을 반올림하여 반환합니다.

Math.ceil(4.7);      //5

 

Math.floor( )

소수 값이 있을 때 소수점 이하 값을 버립니다.(내림)

 

Math.floor(4.7);      //4

 

Math.random( )

0과 1 사이의 임의의 숫자를 랜덤하게 반환합니다. 

항상 1보다 작은 숫자를 반환합니다. ex) 0.54262

Math.random();      //랜덤숫자 0(포함)~1(제외)

 


 

랜덤한 정수를 제작하는 다양한 예시)

Math.floor(Math.random( ))

Math.floor(Math.random() *10) 0 ~ 9 사이 정수
Math.floor(Math.random() * 11); 0~10 사이 정수
Math.floor(Math.random() * 100); 0~99 사이 정수
Math.floor(Math.random() * 101); 0 ~ 100 사이 정수
Math.floor(Math.random() * 10) + 1; 1~10 사이 정수
Math.floor(Math.random() * 100) + 1; 1~100 사이 정수

 

 

 

url www.w3schools.com/js/js_random.asp
 
728x90
  Comments,     Trackbacks