/**
* Math与其他对象不同,它不是一个构造函数
* 它属于一个工具类不用创建对象,它里边封装了数学运算相关的属性和方法
*/
console.log(Math.PI);// 圆周率:3.141592653589793
console.log(Math.abs(-1));//返回绝对值1
console.log(Math.ceil(1.37));//对于一个小数向上取整,2
console.log(Math.floor(1.99));//向下取整,1
console.log(Math.round(1.99));//四舍五入取整,2
/**
* random()生成一个0~1之间随机数
* 生成一个0~10不包括两端的随机数
* Math.round()*10;
* 如果生成0~10之间的随机整数
* Math.round(Math.random()*10)
* 如果生成0~x之间的随机整数
* Math.round(Math.random()*x)
* 如果生成1~10之间的随机整数
* Math.round(Math.random()*9+1)
* 生成一个x~y的随机数
* Math.round(Math.random()*9(y-x)+x)
*/
console.log(Math.random());// 0.1220844289694597
console.log(Math.random() * 10);// 9.93398452428683
console.log(Math.round(Math.random() * 10));//6
console.log(Math.round(Math.random() * 9 + 1));//乘以9是0~9之间随机整数,加一前后区间都加一,就是0~10
/**
* 获取多个数的最大最小值
* max()和min()
*/
console.log(Math.max(1, 5, 3, 2, 9, 0, 2));//9
console.log(Math.min(1, 5, 3, 2, 9, 0, 2));//0
/**
* pow(x,y),返回x的y次幂
*/
console.log(Math.pow(2, 0))//1
/**
* sqrt(x)返回x的平方根
*/
console.log(Math.sqrt(5))// 2.23606797749979
</script>
Math对象一些简单方法
最新推荐文章于 2022-04-27 16:08:52 发布