一、数学处理
系统内置的Math对象
1、Math.PI 圆周率
2、Math.max() 最大值
console.log( Math.max(1,5,9,7,6,4,3,8,2) );
3、Math.min() 最小值
console.log( Math.min(1,5,9,7,6,4,3,8,2) );
4、Math.sin(弧度) 求正弦
1弧度 = 半径
360度的弧度 = 2π * 半径
30度的弧度 = 2π * 半径 / 360 * 角度
//正弦
console.log( Math.sin(30 * 2 * Math.PI / 360) );
5、Math.cos() 求余弦
console.log( Math.cos(60 * Math.PI * 2 / 360) );
6、Math.pow(底数, 幂) 求次方
console.log( Math.pow(2, 3) );
7、Math.sqrt(数字) 开平方根
8、Math.floor() 向下取整 等同于parseInt()
console.log( Math.floor(4.99999) );
9、Math.ceil() 向上取整 ***
console.log( Math.ceil(3.14) );
10、Math.round() 四舍五入
console.log( Math.round(3.14) );
11、Math.random() 随机数 ***
console.log( Math.random() ); // 获取到的结果是 0~1之间的随机小数,可能会得到0,但是永远得不到1
// a~b之间的随机整数
function getRandom(a, b) {
// 判断大小
var max = a
var min = b
if (a < b) {
max = b
min = a
}
var num = parseInt(Math.random() * (max - min)) + min
return num
}
// var n = getRandom(20, 100)
var n = getRandom(10, 5)
console.log(n);
12、Math.abs(正数/负数) 绝对值
进制转换:
1、将十进制转成其他进制
数字.toString(目标进制)
2、将其他进制转成十进制
parseInt(其他进制数字, 当做多少进制)
二、时间日期处理:
时间日期对象: new Date()
①不加实参:获取到当前的时间日期对象
②添加参数,获取指定时间的时间日期对象:
'年-月-日 时:分:秒'
年,月,日,时,分,秒
时间戳