js实例教程-JS中常用的几种Math方法介绍

发布时间:2018-11-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js实例教程-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));                                      //26 alert(Math.ceil(25.5));                                      //26 alert(Math.ceil(25.1));                                      //26   alert(Math.floor(25.9));                                    //25 alert(Math.floor(25.5));                                    //25 alert(Math.floor(25.1));                                    //25   alert(Math.round(25.9));                                          //26 alert(Math.round(25.5));                                          //26 alert(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(&#39; '); }

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

 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(' '); }

4.Math 对象方法

方法 描述
abs(x) 返回 x 的绝对值
acos(x) 返回 x 的反余弦值。
asin(x) 返回 x 的反正弦值。
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
ceil(x) 对数进行上舍入。Math.ceil(25.1)? //26
cos(x) 返回数的余弦。
exp(x) 返回 Ex?的指数。
floor(x) 对 x 进行下舍入。Math.floor(25.9)? //25
LOG(x) 返回数的自然对数(底为e)。
max(x,y,z,...,n) 返回 x,y,z,...,n 中的最高值。
min(x,y,z,...,n) 返回 x,y,z,...,n中的最低值。
pow(x,y) 返回 x 的 y 次幂。
random() 返回 0 ~ 1 之间的随机数。
round(x) 把数四舍五入为最接近的整数。
sin(x) 返回数的正弦。
sqrt(x) 返回数的平方根
tan(x) 返回角的正切。

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));                                      //26 alert(Math.ceil(25.5));                                      //26 alert(Math.ceil(25.1));                                      //26   alert(Math.floor(25.9));                                    //25 alert(Math.floor(25.5));                                    //25 alert(Math.floor(25.1));                                    //25   alert(Math.round(25.9));                                          //26 alert(Math.round(25.5));                                          //26 alert(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(' '); }

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

 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(' '); }

4.Math 对象方法

方法 描述
abs(x) 返回 x 的绝对值。
acos(x) 返回 x 的反余弦值。
asin(x) 返回 x 的反正弦值。
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
ceil(x) 对数进行上舍入。Math.ceil(25.1)? //26
cos(x) 返回数的余弦。
exp(x) 返回 Ex?的指数。
floor(x) 对 x 进行下舍入。Math.floor(25.9)? //25
log(x) 返回数的自然对数(底为e)。
max(x,y,z,...,n) 返回 x,y,z,...,n 中的最高值。
min(x,y,z,...,n) 返回 x,y,z,...,n中的最低值。
pow(x,y) 返回 x 的 y 次幂。
random() 返回 0 ~ 1 之间的随机数。
round(x) 把数四舍五入为最接近的整数。
sin(x) 返回数的正弦。
sqrt(x) 返回数的平方根。
tan(x) 返回角的正切。

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦!&nbsp;js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的js实例教程-JS中常用的几种Math方法介绍全部内容,希望文章能够帮你解决js实例教程-JS中常用的几种Math方法介绍所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。