js math 对象 以及常用api

一。math api

Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,不是某个对象的方法。无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。

取整

//math api 取整console.log(Math.floor(3.99)); // 3 向下取整console.log(parseInt(3.99)); //与math.floor相同,都是向下取整。console.log(Math.ceil(3.01));// 4 向上取整console.log(Math.round(3.4));//四舍五入取整

随机取整

/*console.log( Math.random() );console.log( Math.random() );console.log( Math.random() );//随机取0-1之间的数*///随机取0-9之间的整数console.log(Math.floor(Math.random()*10));console.log(Math.floor(Math.random()*10));console.log(Math.floor(Math.random()*10));

取0-9 之间的整数 要向下取整    random*10就是到10。

var arr=[‘何旭刚‘,‘李思伟‘,‘金娅‘,‘徐坤‘,‘徐佳鑫‘,‘然哥‘,‘汪洋‘,‘李月‘,‘刘德华‘,‘王凌‘];//通过数组的下标 来随机取姓名,数组的下标从0开始。var x=Math.floor(Math.random()*arr.length);console.log(arr[x]);

3.圆周率 Math.PI     最大值 Math.max()  最小值   Math.min()   绝对值 Math.abs()       x的y次方Math.pow(5,3) 5的3次方

//圆周率//console.log( Math.PI );//绝对值//console.log( Math.abs(-3) );//最大值//console.log( Math.max(9,23,78,6,45) );//最小值//console.log( Math.min(9,23,78,6,45) );//x的y次方console.log( Math.pow(5,3) );

 

相关文章