Math对象常用方法(取整细节)

Math 对象

Math 对象用于执行数学任务。

1.常用属性:

  1.E :返回算术常量e,即自然对数的底数(约2.718)

  2.PI :返回圆周率,约3.14159

2.常用方法    Math.方法()  调用即可

  1.abs(x)  返回绝对值

  2.ceil(x)   上舍入

  3.floor(x)  下舍入

  4.round(x)  四舍五入为最近的整数

  5.random()  返回0~1之间的随机数

  6.max(x,y)  返回x,y中最高值

  7.min(x,y)   返回x,y中最低值

  8.pow(x,y)  返回x的y次幂

  9.sqrt(x)    返回x的平方根

  

 向下取整(舍掉小数)

  Math.floor(2)=2
  Math.floor(2.9)=2
  Math.floor(-2.1)=-3
  Math.floor(-2.9)=-3

 向上取整(凑整)

  Math.ceil(2)=2
  Math.ceil(2.1)=3
  Math.ceil(2.5)=3
  Math.ceil(2.9)=3

  Math.ceil(-2)=-2
  Math.ceil(-2.1)=-2
  Math.ceil(-2.5)=-2
  Math.ceil(-2.9)=-2

  四舍五入(取最近整数)

  Math.round(3.14)=3
  Math.round(3.5)=4
  Math.round(-3.14)=-3
  Math.round(-3.51)=-4

 注意:对于 0.5,该方法将进行上舍入

 例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3。

 

  Math.random()返回指定min~max之间的随机数

  var x = Math.floor(Math.random()*(max - min + 1)) + min;

    从数组中的随机获取成员

  var items = [some 成员];

  var randomItem = items[Math.floor(Math.random() * items.length)];

posted @ 2016-04-23 17:51  没错high少是我  阅读(1150)  评论(0编辑  收藏  举报