【问题标题】:How to round up integer division and have int result in Java? [duplicate]如何在 Java 中舍入整数除法并获得 int 结果? [复制]
【发布时间】:2011-11-18 18:57:09
【问题描述】:

我刚刚写了一个小方法来计算手机短信的页数。我没有选择使用Math.ceil 进行四舍五入,老实说,它看起来很丑。

这是我的代码:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   String message = "today we stumbled upon a huge performance leak while optimizing a raycasting algorithm. Much to our surprise, the Math.floor() method took almost half of the calculation time: 3 floor operations took the same amount of time as one trilinear interpolation. Since we could not belive that the floor-method could produce such a enourmous overhead, we wrote a small test program that reproduce";

   System.out.printf("COunt is %d ",(int)messagePageCount(message));



}

public static double messagePageCount(String message){
    if(message.trim().isEmpty() || message.trim().length() == 0){
        return 0;
    } else{
        if(message.length() <= 160){
            return 1;
        } else {
            return Math.ceil((double)message.length()/153);
        }
    }
}

我不太喜欢这段代码,我正在寻找一种更优雅的方式来做这件事。有了这个,我期待 3 而不是 3.0000000。有什么想法吗?

【问题讨论】:

  • 对于大多数业务代码,Math.ceil 更具人类可读性。

标签: java math types formatting


【解决方案1】:

使用Math.ceil() 并将结果转换为int:

  • 这仍然比使用 abs() 避免双打要快。
  • 使用负数时结果是正确的,因为 -0.999 将向上舍入为 0

例子:

(int) Math.ceil((double)divident / divisor);

【讨论】:

  • 这应该是正确答案
  • @Duane OP 说“我没有选择使用 Math.ceil 进行四舍五入”
  • 公平点,虽然我认为这是谷歌现在最热门的“数学四舍五入”:)。可惜它有这种条件。
  • 为什么我们需要在如此简单的计算中进行强制转换? Java 应该提供更智能的 API。
  • 这应该是正确的答案吧?包括浮点计算将不包括它的一些非直观的边缘情况和各种问题,只是为了舍入简单的整数除法?人们经常使用 double 来否定浮动问题,这只会使问题不那么突出,但并不能解决问题introcs.cs.princeton.edu/java/91float
【解决方案2】:

您可以使用整数除法来四舍五入

import static java.lang.Math.abs;

public static long roundUp(long num, long divisor) {
    int sign = (num > 0 ? 1 : -1) * (divisor > 0 ? 1 : -1);
    return sign * (abs(num) + abs(divisor) - 1) / abs(divisor);
}

或者如果两个数字都是正数

public static long roundUp(long num, long divisor) {
    return (num + divisor - 1) / divisor;
}

【讨论】:

  • 我的意思是,试试num=-2div=-3。这以-6/-3=2 结尾,但0,666.. 应四舍五入为1。事实上它不适用于num &lt;= 0 &amp;&amp; div &lt;= 0
  • 我的编译器无法识别 abs() - 我必须使用 Math.abs()...
  • @DougEnglish 那是因为你错过了静态导入。
  • 在这个(第二个)函数中,你需要 num + divisor -1
  • @Peter Lawrey 如果您使用我提到的替代方案,它不会溢出。而且你的第一个函数等于符号 * 带有绝对值的第二个函数,所以没有什么复杂的。
【解决方案3】:

另一种不太复杂的单线:

private int countNumberOfPages(int numberOfObjects, int pageSize) {
    return numberOfObjects / pageSize + (numberOfObjects % pageSize == 0 ? 0 : 1);
}

可以用long代替int;只需更改参数类型和返回类型即可。

【讨论】:

  • 这应该是答案。这可能是最容易实现的方法,并且避免了不执行任何额外不必要的步骤。在转换为不同的数字类型时,它还避免了一些小的加法问题。
  • 很好的解决方案。谢谢
  • 最有效最简单的解决方案。
【解决方案4】:

Google 的 Guava 库handles this in the IntMath class

IntMath.divide(numerator, divisor, RoundingMode.CEILING);

与这里的许多答案不同,它处理负数。尝试除以零时也会引发适当的异常。

【讨论】:

    【解决方案5】:
    (message.length() + 152) / 153
    

    这将给出一个“四舍五入”的整数。

    【讨论】:

      【解决方案6】:
      long numberOfPages = new BigDecimal(resultsSize).divide(new BigDecimal(pageSize), RoundingMode.UP).longValue();
      

      【讨论】:

      • +1 用于使用内置函数
      【解决方案7】:

      如果要计算 a 除以 b 向上取整,可以使用 (a+(-a%b))/b

      【讨论】:

        【解决方案8】:

        扩展 Peter 的解决方案,我发现这对我来说始终可以“朝正无穷大”舍入:

        public static long divideAndRoundUp(long num, long divisor) {
            if (num == 0 || divisor == 0) { return 0; }
        
            int sign = (num > 0 ? 1 : -1) * (divisor > 0 ? 1 : -1);
        
            if (sign > 0) {
                return (num + divisor - 1) / divisor;
            }
            else {
                return (num / divisor);
            }
        }
        

        【讨论】:

        • 除以零不是零,应该是报错。
        【解决方案9】:

        这可能会有所帮助,, 将余数减去legnth,使其成为可整除的数,然后除以153

        int r=message.length()%153;       //Calculate the remainder by %153
        return (message.length()-r)/153;  // find the pages by adding the remainder and 
                                          //then divide by 153 
        

        【讨论】:

        • 如果 message.length() (message.length() + (153 - message.length() % 153)) / 153
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        • 2016-11-02
        • 2016-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多