<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/*
Math
-Math和其他对象不同,他不是一个构造函数
它属于一个工具类不用创建对象,里面封装了数学运算相关的属性和方法
-比如:
Math.PI 表示圆周率
Math.E 表示自然对数的底数
等等
*/
// console.log(Math); //[object Math]
console.log(Math.E); // 2.718281828459045
console.log(Math.PI); // 3.141592653589793
// 1. abs() -可以用来计算一个数的绝对值
console.log(Math.abs(-6)); // 6
/*
2. Math.ceil() -对一个数进行向上取整,小数位只要有值自动进1
3. Math.floor() -对一个数进行向下取整,小数部分会被舍掉
4. round() -对一个数进行四舍五入取整
*/
console.log(Math.ceil(1.3)); // 2
console.log(Math.floor(1.9)); // 1
console.log(Math.round(2.5)); // 3
/*
5. Math.random()
-可以用来生成一个0-1之间的随机数
-包含0但是不包含1
-生成一个0-10的随机数 Math.random()*10
-生成0-x之间的随机数 Math.round(Math.random()*x)
-生成2-10
-生成x-y之间的随机数 Math.round(Math.random()*(y-x)+x
console.log(Math.floor(Math.random()*101) ) //0-100整数
console.log(Math.floor(Math.random()*100+1) ) //1-100整数
console.log(Math.floor(Math.random()*98+3) ) //3-100整数
Math.floor(Math.random()*(b-a+1)+a) // 3(a) - 100(b)
*/
for (var i = 0; i < 10; i++) {
// console.log(Math.round(Math.random() * 10));
// console.log(Math.round(Math.random() * 9) + 1);
}
/*
6. Math.max() -获取多个数中的最大值
7. Math.min() -获取多个数中的最小值
*/
var max = Math.max(10, 50, 20);
var min = Math.min(10, 50, 20);
console.log(min); // 10
// 8. Math.pow(x,y) -返回x的y次幂
// 9. Math.sqrt() -对一个数进行开方
console.log(Math.pow(3, 3)); // 27
console.log(Math.sqrt(4)); // 2
console.log(Math.random().toString(36)) // 0.ctl6gg8zzxc
</script>
</head>
<body>
</body>
</html>
JS中的Math
于 2023-11-30 16:25:54 首次发布