前端笔记77——JS中Math处理数学计算

本文深入讲解了JavaScript中Math对象的功能和使用方法,包括获取圆周率、取绝对值、向上取整、向下取整、求最大值、求最小值、求幂、四舍五入以及生成随机数等操作。通过具体代码示例,读者可以快速掌握Math对象在实际编程中的应用。

JS中Math处理数学计算

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<script type="text/javascript">
			// Math属性,圆周率PI
			console.log(Math.PI)
			// Math的方法
			// 1.abs 取绝对值
			console.log(Math.abs(-1))
			// 2.ceil 向上取整
			console.log(Math.ceil(15.5))
			// 3.floor 向下取整
			console.log(Math.floor(15.5))
			// 4.取最大值
			console.log(Math.max(1, 3, 45, 3, 56))
			// 5.取最小值
			console.log(Math.min(2, 3, 45, 57, 2, 1))
			// 6.求x的y次方
			console.log(Math.pow(2, 3))
			// 7.四舍五入
			console.log(Math.round(9.2))
			console.log(Math.round(9.7))
			// 8.随机数,求0-1之间的随机
			for(var i = 0; i < 30; i++) {
//				console.log(Math.random())
//				Math.random()*(30-10+1) 
				console.log(Math.floor(Math.random()*(30-10+1)+10))
			}
			// 9.如何去求n-m之间的随机整数? 10-30之间的随机整数
			function getRandomInt(min,max){
				// 1把10-30转换成 0-30之间的随机数
				// 2取整数
				return Math.floor(Math.random()*((max-min+1)+min))
			}
		</script>
	</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值