是否有任何 Java 函数或 util 类以这种方式进行四舍五入:func(3/2) = 2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1074228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any Java function or util class which does rounding this way: func(3/2) = 2?
提问by Rig Veda
Is there any Java function or util class
which does rounding this way: func(3/2) = 2
是否有任何 Java 函数或 utilclass
以这种方式进行四舍五入:func(3/2) = 2
Math.ceil()
doesn't help, which by name should have done so. I am aware of BigDecimal
, but don't need it.
Math.ceil()
没有帮助,按名称应该这样做。我知道BigDecimal
,但不需要它。
采纳答案by jjnguy
Math.ceil()
will always round up, however you are doing integer division with 3/2
. Thus, since in integer division 3/2 = 1
(not 1.5
) the ceiling of 1
is 1
.
Math.ceil()
将始终向上取整,但是您正在使用3/2
. 因此,由于在整数除法3/2 = 1
(非1.5
)中, 的上限1
是1
。
What you would need to do to achieve the results you want is Math.ceil(3/2.0);
你需要做的是达到你想要的结果 Math.ceil(3/2.0);
By doing the division by a double amount (2.0
), you end up doing floating point division instead of integer division. Thus 3/2.0 = 1.5
, and the ceil()
of 1.5
is always 2
.
通过按双倍数 ( 2.0
)进行除法,您最终会进行浮点除法而不是整数除法。因此3/2.0 = 1.5
,和ceil()
的1.5
永远是2
。
回答by sisve
Aint this the usual case of integer division? Try Math.Ceil after casting either number to a floating point type.
这不是整数除法的常见情况吗?在将任一数字转换为浮点类型后尝试 Math.Ceil。
回答by PatrikAkerstrand
You can always cast first:
你总是可以先施放:
Math.ceil((double)3/2)
回答by Pesto
Math.ceil willhelp, provided you use floating point numbers. The problem is that 3/2, in integer division, is 1. By the time the value gets to whatever function, be it Math.ceil or something else, the value is simply 1. Any trailing decimal portion is gone.
Math.ceil会有所帮助,前提是您使用浮点数。问题在于整数除法中的 3/2 是 1。当该值到达任何函数时,无论是 Math.ceil 还是其他函数,该值都只是 1。任何尾随小数部分都消失了。
回答by Tal Pressman
A bit of black magic, and you can do it all with integers:
有点黑魔法,你可以用整数来做这一切:
// Divide x by n rounding up
int res = (x+n-1)/n
回答by Tony Miller
Have you tried Math.floor()
?
你试过Math.floor()
吗?
回答by Michael Borgwardt
In Java, 3/2 = 1 because it uses integer division. There's no function that can "fix" this afterwards. What you have to do is to force a float divison and round up the result:
在 Java 中,3/2 = 1,因为它使用整数除法。之后没有任何功能可以“修复”这个问题。您需要做的是强制浮动除法并将结果四舍五入:
int result = (int)Math.ceil( ((float)3) / ((float)2) );
回答by Samuel Carrijo
Many languages "think" like this. If you're dividing an int into an int, then you should get an int (so they truncate and you get 1 as a result).
许多语言都是这样“思考”的。如果您将一个 int 划分为一个 int,那么您应该得到一个 int(因此它们会被截断,结果您得到 1)。
We all know this is not true, but that's how they work. You can "cheat" them, and do something like casting one of them to a double, or use a double representation: Math.ceil (3.0 / 2)
or Math.ceil((double)3/2)
, as mentioned.
我们都知道这不是真的,但这就是他们的工作方式。您可以“欺骗”它们,并执行一些操作,例如将其中一个转换为 double,或者使用 double 表示:Math.ceil (3.0 / 2)
or Math.ceil((double)3/2)
,如上所述。
回答by Brian
if (a % b == 0)
{
return (a / b);
}
else
{
return (a / b) + 1;
}
Exploits integer division to do what you want. I don't know of a math function that does this, but why not roll your own?
利用整数除法来做你想做的事。我不知道有什么数学函数可以做到这一点,但为什么不自己动手呢?
回答by Randy Proctor
To convert floor division to ceiling division:
要将地板分割转换为天花板分割:
(numerator + denominator-1) / denominator
To convert floor division to rounding division:
要将地板除法转换为舍入除法:
(numerator + (denominator)/2) / denominator