数学操作方法
系统内置的Math对象
js内部提供了一个对象,专门用于做数学操作【math】
方法:
🚩随机数
获取到的结果是0~1之间的随机小数,可能会得到0,但是永远得不到1
function sjs(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 num_=sjs(1,10)
console.log(num_)
🚩向上取整
console.log(Math.ceil(3.14))
向下取整
console.log(Math.floor(4.999))
四舍五入
🚩求最大值
🚩求最小值
求次方
开平方根
圆周率
正弦
1弧度=半径
360度的弧度=2Π * 半径
30度的弧度=2Π * 半径 / 360 * 角度
绝对值
进制转换:
将10进制转成其他进制
语法:
数字.toString(目标进制数)
var a=3;
console.log(a.toString(2))
将其他进制转成10进制
语法:
parseInt(要转的数字,将他看做多少进制)
var a='11';
console.log(parseInt(a,2))
时间日期
时间日期
创建这个对象的方法
语法:
var date=new Date()
默认在Date的小括号中没有实参 - 就表示当前时间(指的是当前计算机时间)
如果我们希望得到当前时间,需要在小括号中放实参
实参:
'年-月-日 时:分:秒'
多个数字
年,月,日,时,分,秒
时间戳:使用毫秒数来描述一个时间的
从1970年1月1日0点0分0秒开始计算毫秒数的的
注意:月份中1-12月是用下标0-11来表示的
时间日期对象的作用:
🚩获取:
var date=new Date()
console.log(date.getFullYear())
console.log(date.getMonth())
console.log(date.getDate())
console.log(date.getDay())
console.log(date.getHours())
console.log(date.getMinutes())
console.log(date.getSeconds())
console.log(date.getMilliseconds())
console.log(🚩🚩date.getTime())
设置:
setFullYear()
setMonth()
setDate()
setHours()
setMinutes()
setSeconds()
setTime()