l 比较方法
ü
Math.min()//
求一组数中的最小值
ü
Math.max()//
求一组数中的最大值
ü
Math.min(1,2,19,8,6);//1
l 将小数值舍入为整数的几个方法
ü
Math.ceil()
向上舍入
ü
Math.floor()
向下舍入
ü
Math.round()
四舍五入
Math.ceil(12.41); //13
Math.floor(12.41); //12
Math.round(12.3); //12
Math.round(12.5); //13
l 随机数
ü
Math.random() //
返回大于
0
小于
1
的一个随机数
//求最大值
var a=10,b=20;
var max = a>b?a:b;
if(a>b) var max=a;
else var max=b;
var max = Math.max(a,b);
var max = Math.max(10,30,a,b,1,3,true);
console.log(max);
var min = Math.min(10,30,a,b,1,3,false);
console.log(min);
//四舍五入 round 上ceil 下floor
var num=10.7;
console.log(Math.round(num),Math.ceil(num),Math.floor(num));
//随机数[0,1)之间的小数
console.log(Math.random());
//随机整数 [0~100)
var n1 = parseInt(Math.random()*100);
var n2 = Math.round(Math.random()*100);
var n3 = Math.floor(Math.random()*100);
var arr = [];
for(var i=0;i<90;i++){
//温度 -17~30
//Math.ceil(Math.random()*47)-17
//-170~300 288/10=28.8
var tmp = (Math.ceil(Math.random()*470)-170)/10;
arr.push(tmp);
}
console.log(arr);
网上API:
a) API查看:w3school
https://www.w3school.com.cn/jsref/jsref_obj_string.asp
https://www.w3school.com.cn/jsref/jsref_obj_math.asp
https://www.w3school.com.cn/jsref/jsref_obj_date.asp
b) API学习: mdn
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date
u 其他方法:(了解即可,即用即查)
abs(num) 返回
num
绝对值
exp(num) 返回
Math.E
的
num
次幂
log(num) 返回
num
的自然对数
pow(num,power) 返回
num
的
power
次幂
sqrt(num) 返回
num
的平方根
scos(x) 返回
x
的反余弦值
asin(x) 返回
x
的反正弦值
atan(x) 返回
x
的反正切值
atan2(y,x) 返回
y/x
的反正切值
cos(x) 返回
x
的余弦值
sin(x) 返回
x
的正弦值
tan(x) 返回
x
的正切值