zoukankan      html  css  js  c++  java
  • 容易混淆的某些Math方法说明

    1. Math.round

    返回最接近的整数值,实际上就是我们说的对小数进行四舍五入。

    /**
      * 返回最接近参数的long
      */
    static long round(double a)
    /**
     * 返回最接近参数的int
     */
    static int round(float a)

     实例如下:

    1. Math.round(8.1): 8
    2. Math.round(8.4): 8
    3. Math.round(8.5): 9
    4. Math.round(8.9): 9
    5. Math.round(-8.1): -8
    6. Math.round(-8.4): -8
    7. Math.round(-8.5): -8     // 负数与整数不同。 当第一位小数小于等于5的时候进行进位,因此得到 -8
    8. Math.round(-8.9): -9     // 当第一位小数大于5时,进行舍去。-9是小于-8的下一个整数,因此得到-9

     2. Math.floor

    实际上就是返回不大于参数的最大整数值的double类型。

    /**
     * 返回某个最大的double值。该值小于等于参数,并等于某个整数。
     */
    static double floor(double a) 
     

    实例如下:

    Math.floor(8.9): 8.0
    Math.floor(8.1): 8.0
    Math.floor(-8.1): -9.0
    Math.floor(-8.9): -9.0

     3. Math.ceil

    和Math.floor刚好相反,返回的是不小于参数的最小整数值的double类型。

    /**
     * 返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
     */
    static double ceil(double a)  

    实例如下:

    Math.ceil(8.9): 9.0
    Math.ceil(8.1): 9.0
    Math.ceil(-8.1): -8.0
    Math.ceil(-8.9): -8.0

  • 相关阅读:
    jquery选择器
    frameset的target属性
    最长回文子串 南邮NOJ 1100
    最长回文子串 南邮NOJ 1100
    最长回文子串 南邮NOJ 1100
    NOJ 蛇形填数 1094
    NOJ 蛇形填数 1094
    NOJ 蛇形填数 1094
    NOJ 蛇形填数 1094
    开灯问题 南邮NOJ 1589 (另一种解法)
  • 原文地址:https://www.cnblogs.com/luckyxiaoxuan/p/4452936.html
Copyright © 2011-2022 走看看