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

本文详细介绍了Java中四种常用的数学运算方法:向下取整(Math.floor())、四舍五入(Math.round())、向上取整(Math.ceil())及按最接近的整数取值(Math.rint())。通过具体的示例解释了每种方法的实现原理及其特殊处理情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载来源:http://blog.youkuaiyun.com/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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

由定义可知: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

由定义可以很清楚的知道: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

由定义也可知: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值