Math对象的属性
属性 | 说明 |
---|---|
Math.E | 常量e的值 |
Math.PI | 圆周率 |
一些方法
1. Math.max()方法
var max = Math.max(567, 21, 12, 1);
alert(max); // 567
2. Math.min()方法
var min = Math.min(567, 32, 12, 2);
alert(min); // 2
3. 找到数组最大或最小值
var values = [1, 2, 3, 4, 5, 6, 7, 8];
var max = Math.max.apply(Math, values);
var min = Math.min.apply(Math, values);
舍入方法
将小数值舍入为整数的几个方法:
Math.ceil() 向上舍入
Math.floor() 向下舍入
Math.round() 标准舍入,四舍五入
alert(Math.ceil(25.9)); // 26
alert(Math.ceil(25.5)); // 26
alert(Math.ceil(25.1)); // 26
alert(Math.round(25.9)); // 26
alert(Math.round(25.5)); // 26
alert(Math.round(25.1)); // 25
alert(Math.floor(25.9)); // 25
alert(Math.floor(25.5)); // 25
alert(Math.floor(25.1)); // 25