javascript--Math对象

Math数学对象

Math.min (1, 2, 3);//获取若干个数中的最小值

Math.max (1, 2, 3);//获取若干个数中的最大值

Math.abs (-1.5);//返回数的绝对值

Math.ceil (1.1);// 2 上舍入

Math.floor (1.9);// 1 下舍入

Math.round (1.6);//四舍五入

Math.random ();//0~1的随机数

Math.toSource ();//返回该对象的源代码

 

Math.abs()

eg: var x = -1.5;

//var y = Math.abs(x);

//document.write(y);

document.write(Math.abs(x));

 

求任意范围内随机数的方法为:Math.floor(Math.random() * (b - a + 1) + a);

eg: 求15-25之间的随机数(10个);

var  array1 = [];

for ( var i = 0; i < 10; i++ ) {

     array1[i] = Math.floor( Math.random() * 11 + 15 );

}

for ( var i = 0; i < array1.length; i++ ) {

    document.write( array1[i] );

    //console.log( array1[i] );

}

 

Math.toSource()方法;

eg: 

 

<script type="text/javascript" charset="utf-8">
  function student( name, age, profession) {
    this.name = name;
    this.age = age;
    this.profession = profession;
  }
  var stu = new student('小七', '20', 'major of computer application');
  // var result = stu.toSource();
  document.write(stu.toSource());
</script>

 

posted @ 2016-07-28 10:36  inno  阅读(125)  评论(0编辑  收藏  举报