js--Math对象

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 的正切值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值