zoukankan      html  css  js  c++  java
  • js浮点型,整型操作方法汇总(进行中)

    浮点数操作方法如下:

    1. Math.ceil()用作向上取整。(ceil 天花板)

    var num = Math.ceil(11.02);  //12

    2. Math.floor()用作向下取整。 (floor 地板)

    (js 中取整底层原理是位运算的取反~运算,运用的就是浮点数进行位运算会先转化为整型,例如1.02取反 会舍弃小数部分,~1.02 = -2 ,-2再取反,就是1)

    //浮点数取整
    var num = Math.floor(11.02);  //11
    // 可以用位与运算取整
    var num1 = 11.02 | 0 //11 浮点数没有位运算,会先转为整数然后进行位运算

    3. Math.round() 我们数学中常用到的四舍五入取整。 (round 圆,在..周围,大约)

    Math.round(11.49999);  //11
    Math.round(11.555); //12

    4. numObj.toFixed(num) 方法可把 Number 四舍五入为指定小数位数的数字,num 小数位数。 (tofixed  保留小数)

    //先将数组实例化为对象,然后调用方法,保留小数位时会四舍五入
    
    var num = new Number(11.04);
    num.toFixed(1)  // 11.0
    
    var num1 = new Number(11.05);
    num1.toFixed(1)  // 11.1
    
    11.05.toFixed(1)  //11.1

     5. Math.max(x,..x,..)  比较多个数组,返回最大的值,没有参数则返回 -Infinity,有参数,但不是数字型的返回NAN

      Math.min(x,..,)  比较最小的

    6.Math.random() 返回0-1之间的随机数字

  • 相关阅读:
    [FJWC2018]全排列
    CYJian的新春虐题赛
    C. 新年的繁荣
    CF809E Surprise me!
    codeforces 1110F
    C. mathematican 的二进制
    [SPOJ] DIVCNT2
    CF1065F Up and Down the Tree
    Snakes 的 Naïve Graph
    「LibreOJ Round #9」CommonAnts 的调和数
  • 原文地址:https://www.cnblogs.com/nana-share/p/5015703.html
Copyright © 2011-2022 走看看