zoukankan      html  css  js  c++  java
  • JavaScript中的数学对象Math

    js数学对象Math

    //四舍五入
    var res = Math.round(5.921);
    
    //获取最大值
    var res = Math.max(10,23,523,43,65,46,32,32);
    
    //获取最小值
    var res = Math.min(12312,324,32,42,3,23,412,4332,21,3,-1);
    
    //获取绝对值
    var res = Math.abs(-100);
    
    //退一取整
    var res = Math.floor(1.9);
    
    //进一取整
    var res = Math.ceil(1.1);
    
    //幂运算 用来获取x的y次方 2的3次方
    var res = Math.pow(2,3);
    
    //开方运算 返回一个数的平方根
    var res = Math.sqrt(9);
    random 返回 0 ~ 1 之间的随机数。
    

    random() 返回 0 ~ 1 之间的随机数。

    random 获取一个随机数 返回0-1之间的随机小数 有可能到0 ,但是不会取到1

    //random 获取一个随机数 返回0-1之间的随机小数 有可能到0 ,但是不会取到1
    var res = Math.random();
    
    //0-9随机数 小数
    var res = Math.random()*10;
    
    //0-9随机整数 (9-0 +1) +0
    var res = Math.floor(Math.random()*10);
    
    //1-10随机整数(10-1 +1) +1
    var res = Math.floor(Math.random()*10)+1;
    
    //0-10随机整数(10-0 +1) +0
    var res = Math.floor(Math.random()*11)+0;
    
    //5-15随机整数(15-5 +1) +5
    var res = Math.floor(Math.random()*11)+5;
    
    //3-99 (99-3 +1) +3
    
    //封装函数
    function rand(m,n){
        return Math.floor(Math.random()*(n-m+1))+m;
    }
    var res = rand(20,30);
    
    console.log(res);
    
  • 相关阅读:
    左右布局,中分线可以左右拖动
    ES6
    nodeclub route
    nodeclub models
    mybatis中sql语句查询操作
    mybatis开发dao的方式
    Mybatis框架入门
    Maven
    Android开发-ADT Bundle安装
    人类史一览
  • 原文地址:https://www.cnblogs.com/victorfengming/p/11930977.html
Copyright © 2011-2022 走看看