JS入门之Math对象

Math: 数学,内置对象,在ECMAScript程序执行之前就已存在了,保存了数学公式和信息的对象

Math对象上的方法:

1. 找出最大值, 最小值

Math.max(多个需要比较的值); // 返回最大值
Math.min(多个需要比较的值); // 返回最小值

扩展:
Math.max.apply(Math, [5,6,7])

2. 舍入方法

Math.ceil(数字); 向上取整
Math.floor(数字); 向下取整
Math.round(数字); 四舍五入

3. 随机数

Math.random(); 0 ~ 1;

function random(start, end) {
return Math.floor(Math.random() * (end - start + 1) + start);
}

4. 绝对值

Math.abs(数字)

5. 次幂

Math.pow(num, power): num的power次幂

6. 平方根

Math.sqrt(num); 平方根

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容