我怎么能总是将一个双向舍入到一个int,而不是将它向下舍入。 我知道 Math.round(double),但我希望它总是向上舍入。 因此,如果它是 3.2 ,它会四舍五入为4.
How could I always round UP a double to an int, and never round it down. I know of Math.round(double), but I want it to always round up. So if it was 3.2, it gets rounded to 4.
这是否可能?
推荐答案您可以使用 Math.ceil()方法。
参见JavaDoc链接: docs.oracle/javase/10/docs/api/java/lang/Math.html#ceil(double)
See JavaDoc link: docs.oracle/javase/10/docs/api/java/lang/Math.html#ceil(double)
来自文档:
ceil
public static double ceil(double a)返回大于或等于参数且等于数学整数的最小(最接近负无穷大)double值。特殊情况:
Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:
- 如果参数值已经等于数学整数,则结果与参数相同。
- 如果参数为NaN或无穷大或正零或负零,则结果与参数相同。
- 如果参数值小于大于-1但大于-1.0,则结果为负零。
请注意,Math.ceil(x)的值为确切地说-Math.floor(-x)的值。
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
参数:
- a - 一个值。
返回:
最大(最接近负无穷大)浮点值,该值大于或等于参数且等于数学整数。
The smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.
更多推荐
总是圆了一双
发布评论