Android中Math類Math.floor()、Math.round()及Math.ceil()等方法的使用

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/isee361820238/article/details/52369890

1、Math.floor()

先看定義:

/**
 * Returns the double conversion of the most positive (closest to positive
 * infinity) integer value less than or equal to the argument.
 * <p>
 * Special cases:
 * <ul>
 * <li>{@code floor(+0.0) = +0.0}</li>
 * <li>{@code floor(-0.0) = -0.0}</li>
 * <li>{@code floor(+infinity) = +infinity}</li>
 * <li>{@code floor(-infinity) = -infinity}</li>
 * <li>{@code floor(NaN) = NaN}</li>
 * </ul>
 */
public static native double floor(double d);

由定義可知:Math.floor()表示向下取整。,即小於或等於double型參數的整數位的數字。
比如:Math.floor(18.7)的結果是18.0,Math.floor(-18.3)的結果-19.0,下面再列出幾組:

Math.floor(-1.1): -2.0
Math.floor(-1.5): -2.0
Math.floor(-1.6): -2.0
Math.floor(0.1): 0.0
Math.floor(0.5): 0.0
Math.floor(0.6): 0.0
Math.floor(1.1): 1.0
Math.floor(1.5): 1.0
Math.floor(1.6): 1.0

2、Math.round()

接下來看Math.round()的定義:

/**
 * Returns the result of rounding the argument to an integer. The result is
 * equivalent to {@code (int) Math.floor(f+0.5)}.
 * <p>
 * Special cases:
 * <ul>
 * <li>{@code round(+0.0) = +0.0}</li>
 * <li>{@code round(-0.0) = +0.0}</li>
 * <li>{@code round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE}</li>
 * <li>{@code round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE}</li>
 * <li>{@code round(+infinity) = Integer.MAX_VALUE}</li>
 * <li>{@code round(-infinity) = Integer.MIN_VALUE}</li>
 * <li>{@code round(NaN) = +0.0}</li>
 * </ul>
 *
 * @param f
 *            the value to be rounded.
 * @return the closest integer to the argument.
 */
public static int round(float f) {
    // check for NaN
    if (f != f) {
        return 0;
    }
    return (int) floor(f + 0.5f);
}

由定義可以很清楚的知道:Math.round()方法表示的是“四捨五入”的計算。
算法爲Math.floor(f+0.5),即將原來的數字加上0.5後再向下取整
Math.round(18.5)的結果爲19,Math.round(-18.5)的結果爲-18。下面再列出幾組:

Math.round(-1.1): -1
Math.round(-1.5): -1
Math.round(-1.6): -2
Math.round(0.1): 0
Math.round(0.5): 1
Math.round(0.6): 1
Math.round(1.1): 1
Math.round(1.5): 2
Math.round(1.6): 2

3、Math.ceil()

Math.ceil的定義爲:

/**
 * Returns the double conversion of the most negative (closest to negative
 * infinity) integer value greater than or equal to the argument.
 * <p>
 * Special cases:
 * <ul>
 * <li>{@code ceil(+0.0) = +0.0}</li>
 * <li>{@code ceil(-0.0) = -0.0}</li>
 * <li>{@code ceil((anything in range (-1,0)) = -0.0}</li>
 * <li>{@code ceil(+infinity) = +infinity}</li>
 * <li>{@code ceil(-infinity) = -infinity}</li>
 * <li>{@code ceil(NaN) = NaN}</li>
 * </ul>
 */
public static native double ceil(double d);

由定義也可知:Math.ceil()方法就表示向上取整。即大於或等於double型參數的整數位的數字。
比如:Math.ceil(18.3)的結果是19.0,Math.ceil(-18.7)的結果-18.0。再看看一組:

Math.ceil(-1.1): -1.0
Math.ceil(-1.5): -1.0
Math.ceil(-1.6): -1.0
Math.ceil(0.1): 1.0
Math.ceil(0.5): 1.0
Math.ceil(0.6): 1.0
Math.ceil(1.1): 2.0
Math.ceil(1.5): 2.0
Math.ceil(1.6): 2.0

4、Math.rint()

Math.rint()的定義:

/**
 * Returns the double conversion of the result of rounding the argument to
 * an integer. Tie breaks are rounded towards even.
 * <p>
 * Special cases:
 * <ul>
 * <li>{@code rint(+0.0) = +0.0}</li>
 * <li>{@code rint(-0.0) = -0.0}</li>
 * <li>{@code rint(+infinity) = +infinity}</li>
 * <li>{@code rint(-infinity) = -infinity}</li>
 * <li>{@code rint(NaN) = NaN}</li>
 * </ul>
 *
 * @param d
 *            the value to be rounded.
 * @return the closest integer to the argument (as a double).
 */
public static native double rint(double d);

Math.ring()返回double值最接近參數的值,並等於某個整數。如果兩個double值跟整數都同樣接近,結果是整數值是偶數。特殊情況:

如果參數值已經等於某個整數,那麼結果跟參數一樣。

如果參數爲NaN或無窮大,正零或負零,那麼結果和參數一樣。
比如例子:

Math.rint(-1.1): -1.0
Math.rint(-1.5): -2.0
Math.rint(-1.6): -2.0
Math.rint(0.1): 0.0
Math.rint(0.5): 0.0
Math.rint(0.6): 1.0
Math.rint(1.1): 1.0
Math.rint(1.5): 2.0
Math.rint(1.6): 2.0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章