是否有任何Java函数或util类以这种方式舍入:func(3/2)= 2?

时间:2021-10-20 03:59:34

Is there any Java function or util class which does rounding this way: func(3/2) = 2

是否存在以这种方式舍入的Java函数或util类: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,但不需要它。

15 个解决方案

#1


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,并且1.5的ceil()总是2。

#2


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

#3


You can always cast first:

你总是可以先施展:

Math.ceil((double)3/2)

#4


To convert floor division to ceiling division:

将楼层划分转换为上限划分:

(numerator + denominator-1) / denominator

To convert floor division to rounding division:

将楼层划分转换为舍入划分:

(numerator + (denominator)/2) / denominator

#5


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,因为它使用整数除法。之后没有任何功能可以“修复”此功能。你要做的是强制浮动divison并将结果四舍五入:

int result = (int)Math.ceil( ((float)3) / ((float)2) );

#6


Aint this the usual case of integer division? Try Math.Ceil after casting either number to a floating point type.

这是整数除法的通常情况吗?在将任一数字转换为浮点类型后尝试Math.Ceil。

#7


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的方法,或者使用双重表示:Math.ceil(3.0 / 2)或Math.ceil((double)3/2),如上所述。

#8


Math.ceil will help, 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.任何尾随小数部分都消失了。

#9


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?

利用整数除法来做你想要的。我不知道这样做的数学函数,但为什么不自己动手?

#10


below fragment works with negative integers as well:

下面的片段也适用于负整数:

public static int divRoundUp(int x, int n) {
    if (n<=0) throw new RuntimeException("conceived wt. pos. dividers (was:"+n+")");
    int ret = (x+(n-1)*(x>0?1:0))/n;
    return ret;
}

#11


I like Randy Proctor's answer the best. Here in more detail:

我最喜欢Randy Proctor的答案。在这里更详细:

If you want to do real rounding (i.e. 3/2 -> 2, but 17 / 7 -> 2) with integers > 0: use (dividend + (divisor / 2)) / divisor instead of dividend / divisor.

如果你想用整数> 0进行真正的舍入(即3/2 - > 2,但是17/7 - > 2):使用(dividend +(divisor / 2))/ divisor而不是dividend / divisor。

If dividend can be any integer (i.e. negative allowed): (dividend >= 0) ? ((dividend + divisor / 2) / divisor) : ((dividend - divisor / 2) / divisor).

如果被除数可以是任何整数(即允许负数):(被除数> = 0)? ((被除数+除数/ 2)/除数):((被除数 - 除数/ 2)/除数)。

If dividend is any integer and divisor any integer but 0: (dividend >= 0) ? ((dividend + Math.abs(divisor) / 2) / divisor) : ((dividend - Math.abs(divisor) / 2) / divisor).

如果被除数是任何整数,除数是任何整数但是0 :(被除数> = 0)? ((被除数+ Math.abs(除数)/ 2)/除数):((被除数 - Math.abs(除数)/ 2)/除数)。

(Note that the addition and substraction can cause a wraparound that otherwise wouldn't occur, rendering the result incorrect.)

(请注意,加法和减法可能会导致环绕,否则不会发生,导致结果不正确。)

#12


Here is a method I created to handle int division without using Math Round and casting to float. This works for positive and negative numbers. It works by adding half of the denominator to offset the rounding down

这是我创建的一个方法来处理int除法而不使用Math Round和cast to float。这适用于正数和负数。它的工作原理是加上一半的分母来抵消四舍五入

public static int div_Int(int num, int den){
    if(num > 0 && den > 0 || num < 0 && den < 0 ){
        return ((2*num)+ den)/(2*den);  
    }else{
        return ((2*num)- den)/(2*den);
    }

}

#13


If you want to just divide by 2, you can do:

如果你想除以2,你可以这样做:

n - n / 2

And in general:

总的来说:

(n - 1) / d + 1 == (n + d - 1) / d

This holds for non-negative integers. How to extend it to negative integers depends on what you mean with "does rounding this way". Integer division is rounded towards zero, whereas Math.ceil() rounds up and Math.floor() rounds down. For example n / 2 != (int) Math.floor(n / 2.0) for n == -5.

这适用于非负整数。如何将它扩展为负整数取决于你的意思是“舍入这种方式”。整数除法向零舍入,而Math.ceil()向上舍入,Math.floor()向下舍入。例如,对于n == -5,n / 2!=(int)Math.floor(n / 2.0)。

If you want to always round up, you can use Math.ceil() as in this answer.

如果你想要总是向上舍入,你可以在这个答案中使用Math.ceil()。

#14


If you really want to avoid using ceil and casting, here is a little method that accomplishes the same thing.

如果你真的想避免使用ceil和cast,这里有一个小方法来完成同样的事情。

public int findCeil(int X, int Y) {
        if (X % Y == 0){
            return X / Y;
        } else {
            return X / Y + 1;
        }
    }

#15


Have you tried Math.floor() ?

你试过Math.floor()吗?

#1


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,并且1.5的ceil()总是2。

#2


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

#3


You can always cast first:

你总是可以先施展:

Math.ceil((double)3/2)

#4


To convert floor division to ceiling division:

将楼层划分转换为上限划分:

(numerator + denominator-1) / denominator

To convert floor division to rounding division:

将楼层划分转换为舍入划分:

(numerator + (denominator)/2) / denominator

#5


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,因为它使用整数除法。之后没有任何功能可以“修复”此功能。你要做的是强制浮动divison并将结果四舍五入:

int result = (int)Math.ceil( ((float)3) / ((float)2) );

#6


Aint this the usual case of integer division? Try Math.Ceil after casting either number to a floating point type.

这是整数除法的通常情况吗?在将任一数字转换为浮点类型后尝试Math.Ceil。

#7


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的方法,或者使用双重表示:Math.ceil(3.0 / 2)或Math.ceil((double)3/2),如上所述。

#8


Math.ceil will help, 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.任何尾随小数部分都消失了。

#9


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?

利用整数除法来做你想要的。我不知道这样做的数学函数,但为什么不自己动手?

#10


below fragment works with negative integers as well:

下面的片段也适用于负整数:

public static int divRoundUp(int x, int n) {
    if (n<=0) throw new RuntimeException("conceived wt. pos. dividers (was:"+n+")");
    int ret = (x+(n-1)*(x>0?1:0))/n;
    return ret;
}

#11


I like Randy Proctor's answer the best. Here in more detail:

我最喜欢Randy Proctor的答案。在这里更详细:

If you want to do real rounding (i.e. 3/2 -> 2, but 17 / 7 -> 2) with integers > 0: use (dividend + (divisor / 2)) / divisor instead of dividend / divisor.

如果你想用整数> 0进行真正的舍入(即3/2 - > 2,但是17/7 - > 2):使用(dividend +(divisor / 2))/ divisor而不是dividend / divisor。

If dividend can be any integer (i.e. negative allowed): (dividend >= 0) ? ((dividend + divisor / 2) / divisor) : ((dividend - divisor / 2) / divisor).

如果被除数可以是任何整数(即允许负数):(被除数> = 0)? ((被除数+除数/ 2)/除数):((被除数 - 除数/ 2)/除数)。

If dividend is any integer and divisor any integer but 0: (dividend >= 0) ? ((dividend + Math.abs(divisor) / 2) / divisor) : ((dividend - Math.abs(divisor) / 2) / divisor).

如果被除数是任何整数,除数是任何整数但是0 :(被除数> = 0)? ((被除数+ Math.abs(除数)/ 2)/除数):((被除数 - Math.abs(除数)/ 2)/除数)。

(Note that the addition and substraction can cause a wraparound that otherwise wouldn't occur, rendering the result incorrect.)

(请注意,加法和减法可能会导致环绕,否则不会发生,导致结果不正确。)

#12


Here is a method I created to handle int division without using Math Round and casting to float. This works for positive and negative numbers. It works by adding half of the denominator to offset the rounding down

这是我创建的一个方法来处理int除法而不使用Math Round和cast to float。这适用于正数和负数。它的工作原理是加上一半的分母来抵消四舍五入

public static int div_Int(int num, int den){
    if(num > 0 && den > 0 || num < 0 && den < 0 ){
        return ((2*num)+ den)/(2*den);  
    }else{
        return ((2*num)- den)/(2*den);
    }

}

#13


If you want to just divide by 2, you can do:

如果你想除以2,你可以这样做:

n - n / 2

And in general:

总的来说:

(n - 1) / d + 1 == (n + d - 1) / d

This holds for non-negative integers. How to extend it to negative integers depends on what you mean with "does rounding this way". Integer division is rounded towards zero, whereas Math.ceil() rounds up and Math.floor() rounds down. For example n / 2 != (int) Math.floor(n / 2.0) for n == -5.

这适用于非负整数。如何将它扩展为负整数取决于你的意思是“舍入这种方式”。整数除法向零舍入,而Math.ceil()向上舍入,Math.floor()向下舍入。例如,对于n == -5,n / 2!=(int)Math.floor(n / 2.0)。

If you want to always round up, you can use Math.ceil() as in this answer.

如果你想要总是向上舍入,你可以在这个答案中使用Math.ceil()。

#14


If you really want to avoid using ceil and casting, here is a little method that accomplishes the same thing.

如果你真的想避免使用ceil和cast,这里有一个小方法来完成同样的事情。

public int findCeil(int X, int Y) {
        if (X % Y == 0){
            return X / Y;
        } else {
            return X / Y + 1;
        }
    }

#15


Have you tried Math.floor() ?

你试过Math.floor()吗?