Math对象用于执行数学任务。
Math对象并不像Date和String那样是对象的类,因此没有构造函数Math()。
Math常用属性
Math.PI
PI就是圆周率,方便我们计算圆的各种运算
console.log(Math.PI);//3.141592653589793
Math常用方法
注意:接收参数为number类型,如使用非number类型则方法会尝试使用Number(num)方法去转换为Number类型,而不是使用parseFloat(num)
- Math.abs(num);取一个数的绝对值
Math.abs(7.5);//7.5
Math.abs(-7.5);//7.5
Math.abs(null);//0
Math.abs("Hello");//NaN
Math.abs(2+3);//5
Math.abs(0);//0
- Math.round(num);取最近的整数。注意,num为正数时,就是四舍五入,但是,num为负数时,并不是真正意义上的四舍五入。
Math.round(2.60);//3
Math.round(2.50);//3
Math.round(2.49);//2
Math.round(-2.60);