javaScript系列---【Math函数-基本语法】

Math数学函数

Math属于js中的内置对象 (内置对象的方法都是用当前类(Math).出的)

基本语法:

 Math.方法(参数);

Math.abs(数据); 取绝对值

Math.floor(数据); 向下取整

Math.ceil(数据);向上取整

Math.max(数据1,数据2,..); 取最大值

Math.min(数据1,数据2,..); 取最小值

Math.pow(n,m); n的m次幂

Math.sqrt(数据); 开方

Math.round(数据);四舍五入

Math.random(); 生成[0-1)之间的随机小数 左闭右开区间 包含0,不包含1

生成某个范围的随机数 (n-m)

  • Math.round(Math.random()*(m-n)+n);

console.log(Math.abs(-10));
console.log(Math.abs(-10.23));
console.log(Math.abs(10.23));
  • 生成0-n某个范围 Math.round(Math.random()*n)

console.log(Math.round(Math.random()*(5-0)+0));
        0-10
  • 既包括开始值也包括结束值

 

posted on 2021-03-17 17:38  码农小小海  阅读(173)  评论(0编辑  收藏  举报

导航