Frontend/JavaScript

[JavaScript]Math 클래스 함수

sukii 2024. 1. 8. 18:16
반응형

⚫Math 클래스 함수
수학 관련 프로퍼티와 메소드를 제공하기 위한 클래스 함수

 

실습 예제👩‍💻

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JavaScript</title>
</head>
<body>
	<script type="text/javascript">
	//Math.PI : 원주율이 저장된 프로퍼티
	alert("Math.PI = "+Math.PI);//Math.PI = 3.141592653589793
	
	//Math.ceil(number) : 매개변수로 전달받은 숫자값에 소수점 자리의 값이 있는 경우 올림
	//처리된 정수값으로 변환하여 반환하는 메소드
	alert("Math.ceil(12.1) = "+Math.ceil(12.1));//Math.ceil(12.1) = 13
	
	//Math.floor(number) : 매개변수로 전달받은 숫자값에 소수점 자리의 값이 있는 경우 버림(내림)
	//처리된 정수값으로 변환하여 반환하는 메소드
	alert("Math.floor(12.9) = "+Math.floor(12.9));//Math.floor(12.9) = 12
	
	//Math.round(number) : 매개변수로 전달받은 숫자값에 소수점 자리의 값이 있는 경우 반올림
	//처리된 정수값으로 변환하여 반환하는 메소드
	alert("Math.round(12.4) = "+Math.round(12.4));//Math.round(12.4) = 12
	alert("Math.round(12.5) = "+Math.round(12.5));//Math.round(12.5) = 13
	
	//Math.pow(number ,number) : 매개변수로 전달받은 숫자값에 대한 제곱근을 계산하여 반환하는 메소드
	alert("Math.pow(3,5) = "+Math.pow(3,5));//Math.pow(3,5) = 243
	
	//Math.random() : 0.0보다 크거나 같고 1.0보다 작은 실수 난수값을 반환하는 메소드
	alert("난수값 = "+parseInt(Math.random()*100));//0~99 범위의 정수 정수 난수값 반환
	</script>
</body>
</html>

 

반응형