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

4413人阅读 评论(0)

### 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(-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()

/**
* 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(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(-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值跟整数都同样接近，结果是整数值是偶数。特殊情况:

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
4
0

个人资料
• 访问：65252次
• 积分：998
• 等级：
• 排名：千里之外
• 原创：30篇
• 转载：1篇
• 译文：2篇
• 评论：6条
博客专栏
 Android开发之多线程 文章：3篇 阅读：3385
阅读排行
最新评论