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>