星空网 > 软件开发 > Java

js中常用的Math方法总结

1.min()和max()方法

Math.min()用于确定一组数值中的最小值。Math.max()用于确定一组数值中的最大值。

alert(Math.min(2,4,3,6,3,8,0,1,3));              //最小值alert(Math.max(4,7,8,3,1,9,6,0,3,2));         //最大值

2.舍入方法

Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;

Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;

Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数;

例如:

alert(Math.ceil(25.9));                   //26alert(Math.ceil(25.5));                   //26alert(Math.ceil(25.1));                   //26 alert(Math.floor(25.9));                  //25alert(Math.floor(25.5));                  //25alert(Math.floor(25.1));                  //25 alert(Math.round(25.9));                     //26alert(Math.round(25.5));                     //26alert(Math.round(25.1));                     //25

3.random()方法

Math.random()方法返回介于0到1之间一个随机数,不包括0和1。如果想大于这个范围的话,可以套用一下公式:

值 = Math.floor(Math.random() * 总数 + 第一个值)

例如:

alert(Math.floor(Math.random() * 10 + 1));    //随机产生1-10之间的任意数

for (var i = 0; i<10;i ++) {    document.write(Math.floor(Math.random() * 10 + 5));       //5-14之间的任意数    document.write('<br />');}

为了更加方便的传递想要范围,可以写成函数

function selectFrom(lower, upper) {    var sum = upper - lower + 1;                      //总数-第一个数+1    return Math.floor(Math.random() * sum + lower);} for (var i=0 ;i<10;i++) {    document.write(selectFrom(5,10));                 //直接传递范围即可    document.write('<br />');}

4.其它方法

如下表格:

方  法

说  明

Math.abs(num)

返回num的绝对值

Math.exp(num)

返回Math.E的num次幂

Math.log(num)

返回num的自然对数

Math.pow(num,power)

返回num的power次幂

Math.sqrt(num)

返回num的平方根

Math.acos(x)

返回x的反余弦值

Math.asin(x)

返回x的反正弦值

Math.atan(x)

返回x的反正切值

Math.atan2(y,x)

返回y/x的反正切值

Math.cos(x)

返回x的余弦值

Math.sin(x)

返回x的正弦值

Math.tan(x)

返回x的正切值


 

 




原标题:js中常用的Math方法总结

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

了解国际商标知识产权局的权威性:https://www.kjdsnews.com/a/1360175.html
了解注册亚马逊商标的时间节奏及流程:https://www.kjdsnews.com/a/1360176.html
了解欧盟注册商标的证书类型:https://www.kjdsnews.com/a/1360177.html
了解欧盟注册商标的认证证书:https://www.kjdsnews.com/a/1360178.html
了解欧盟商标的类别和申请要求:https://www.kjdsnews.com/a/1360179.html
了解欧盟商标申请的必要条件:https://www.kjdsnews.com/a/1360180.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流