zoukankan      html  css  js  c++  java
  • JS:Math函数的常用方法

    Math函数方法有很多,用 console.dir(Math) 可以在浏览器控制台中查看所有的Math方法.

    这里主要整理一下常用的Math函数方法

    1、 Math.ceil() and Math.floor() 向上取整和向下取整。 (ceil有天花板的意思,向上;floor是地板的意思,向下)

       console.log(Math.ceil(8.11));//9
       console.log(Math.floor(8.11));//8
    

    2、 Math.round() 四舍五入。 (注意:正数时,包含5是向上取整,负数时 ,包含5是向下取整(因为负数时往下取更大)。)

      Math.round(-8.3) = -8
      Math.round(-8.5) = -8
      Math.round(-8.51) = -9
    

    3、 Math.random() 取[0-1) 的随机数 (要扩大区间需在后面添加运算) (注意:左闭右开 !)

      例1,取[1 ,10)和[1,10]
      console.log(parseInt(Math.random()*10));    //未包含10 [0,10)
      console.log(parseInt(Math.random()*(10+1)));    //包含10 [0,11)
      例2,取 [n, m]
      Math.round(Math.random()*(m-n)+n)    
    

    4、 Math.abs() 获取绝对值

      Math.abs(-8) = 8
    

    5、 Math.max() and Max.min() 获取一组数据中的最大值和最小值

      console.log(Math.max(10,1,9,100,200,45,78)); // 200
      console.log(Math.min(10,1,9,100,200,45,78)); // 1
    

    6,Math.PI 获取圆周率π 的值

      console.log(Math.PI);
    

    7,Math.pow() and Math.sqrt()

      Math.pow()获取一个值的多少次幂 
      Math.sqrt()对数值开方
    
      1.Math.pow(10,2) = 100;
      2.Math.sqrt(100) = 10;
    

  • 相关阅读:
    Executors源码之线程池
    Java序列化对字段名的影响
    Spring Cloud Alibaba(二)
    Security版本冲突,老版本共用服务接入新版本服务
    记一次虚拟机崩溃事件和解决方法(CentOS7)
    vue-cli 项目构建学习笔记(Vue3)
    IDEA插件-IDE Eval Reset
    Docker的学习
    Spring Security的学习
    Spring MVC框架的设计理念
  • 原文地址:https://www.cnblogs.com/WayToGo-hwd/p/13991614.html
Copyright © 2011-2022 走看看