总是圆了一双 [英] Always Round UP a Double

查看:107
本文介绍了总是圆了一双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能总是将一个双向舍入到一个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链接: https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#ceil(double)

See JavaDoc link: https://docs.oracle.com/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.

这篇关于总是圆了一双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆