29

我来到我的java程序的一部分,我需要四舍五入到最接近的一百,并认为可能有某种方法可以做到这一点,但我想没有。所以我在网上搜索示例或任何答案,但我还没有找到任何答案,因为所有示例似乎都是最接近的一百个。我只是想这样做并四舍五入。也许有一些我忽略的简单解决方案。我已经尝试过Math.ceil其他功能,但尚未找到答案。如果有人可以帮助我解决这个问题,我将不胜感激。

如果我的数字是 203,我希望结果四舍五入为 300。你明白了。

  1. 801->900
  2. 99->100
  3. 14->100
  4. 452->500
4

11 回答 11

63

Take advantage of integer division, which truncates the decimal portion of the quotient. To make it look like it's rounding up, add 99 first.

int rounded = ((num + 99) / 100 ) * 100;

Examples:

801: ((801 + 99) / 100) * 100 → 900 / 100 * 100 → 9 * 100 = 900
99 : ((99 + 99) / 100) * 100 → 198 / 100 * 100 → 1 * 100 = 100
14 : ((14 + 99) / 100) * 100 → 113 / 100 * 100 → 1 * 100 = 100
452: ((452 + 99) / 100) * 100 → 551 / 100 * 100 → 5 * 100 = 500
203: ((203 + 99) / 100) * 100 → 302 / 100 * 100 → 3 * 100 = 300
200: ((200 + 99) / 100) * 100 → 299 / 100 * 100 → 2 * 100 = 200

Relevant Java Language Specification quote, Section 15.17.2:

Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d · q| ≤ |n|.

于 2013-08-23T16:29:52.667 回答
11

这是我认为适用于任何“多个”情况的算法。让我知道你的想法。

int round (int number,int multiple){

    int result = multiple;

    //If not already multiple of given number

    if (number % multiple != 0){

        int division = (number / multiple)+1;

        result = division * multiple;

    }

    return result;

}
于 2013-08-24T07:10:30.863 回答
9

尝试这个:

(int) (Math.ceil(number/100.0))*100
于 2017-11-27T23:58:58.627 回答
4
int roundUpNumberByUsingMultipleValue(double number, int multiple) {

        int result = multiple;

        if (number % multiple == 0) {
            return (int) number;
        }

        // If not already multiple of given number

        if (number % multiple != 0) {

            int division = (int) ((number / multiple) + 1);

            result = division * multiple;

        }
        return result;

    }

Example:
System.out.println("value 1 =" + round(100.125,100));   
System.out.println("value 2 =" + round(163,50));
System.out.println("value 3 =" + round(200,100));
System.out.println("value 4 =" + round(235.33333333,100));
System.out.println("value 5 =" + round(0,100));

OutPut: 
value 1 =200
value 2 =200
value 3 =200
value 4 =300
value 5 =0
于 2014-07-19T13:54:16.573 回答
4
long i = 2147483648L;
if(i % 100 != 0) {
   long roundedI = (100 - (i % 100)) + i;
}

例子:

649: (100 - (649 % 100)) + 649 -> (100 - 49) + 649) -> 51 + 649 = 700
985: (100 - (985 % 100)) + 985 -> (100 - 85) + 985) -> 15 + 985 = 1000

Long 数据类型用于确保整数范围的限制不会对较大的值造成任何问题。例如,这在金额值(银行域)的情况下可能非常重要。

于 2016-10-01T20:46:21.740 回答
3

另一种方法是使用 BigDecimal

private static double round(double number, int precision, RoundingMode roundingMode) {
    BigDecimal bd = null;
    try {
        bd = BigDecimal.valueOf(number);
    } catch (NumberFormatException e) {
        // input is probably a NaN or infinity
        return number;
    }
    bd = bd.setScale(precision, roundingMode);
    return bd.doubleValue();
}

round(102.23,0,RoundingMode.UP) = 103  
round(102.23,1,RoundingMode.UP) = 102.3  
round(102.23,2,RoundingMode.UP) = 102.24  
round(102.23,-1,RoundingMode.UP) = 110  
round(102.23,-2,RoundingMode.UP) = 200  
round(102.23,-3,RoundingMode.UP) = 1000
于 2017-04-20T20:25:54.963 回答
1

我没有足够的声誉来为OC的答案添加评论,但我认为应该是:

if (number % multiple != 0) {
    int division = (number / multiple) + 1;
    result = division * multiple;
} else {
    result = Math.max(multiple, number);
}

用 theelse这样,例如round(9, 3) = 9,否则它将是round(9, 3) = 3

于 2016-09-21T09:33:14.420 回答
1

rgettman trunaction 的简单实现:

public class Main {

    private static int roundUp(int src) {
        int len = String.valueOf(src).length() - 1;
        if (len == 0) len = 1;
        int d = (int) Math.pow((double) 10, (double) len);
        return (src + (d - 1)) / d * d;
    }

    public static void main(String[] args)  {
        System.out.println("roundUp(56007) = " + roundUp(56007));
        System.out.println("roundUp(4511) = " + roundUp(4511));
        System.out.println("roundUp(1000) = " + roundUp(1000));
        System.out.println("roundUp(867) = " + roundUp(867));
        System.out.println("roundUp(17) = " + roundUp(17));
        System.out.println("roundUp(5) = " + roundUp(5));
        System.out.println("roundUp(0) = " + roundUp(0));
    }
}

输出:

roundUp(56007) = 60000
roundUp(4511) = 5000
roundUp(1000) = 1000
roundUp(867) = 900
roundUp(17) = 20
roundUp(5) = 10
roundUp(0) = 0
于 2016-10-24T07:10:58.737 回答
0

下面的代码适用于将整数四舍五入到下一个 10 或 100 或 500 或 1000 等。

public class MyClass {
    public static void main(String args[]) {
        int actualValue = 34199;
        int nextRoundedValue = 500 // change this based on your round requirment ex: 10,100,500,...
        int roundedUpValue = actualValue;

        //Rounding to next 500
        if(actualValue%nextRoundedValue != 0)
          roundedUpValue =
(((actualValue/nextRoundedValue)) * nextRoundedValue) + nextRoundedValue;
         System.out.println(roundedUpValue);
    }
}
于 2018-02-28T05:55:24.940 回答
0

这对我来说非常有效:

    var round100 = function(n) {
        if (n < 50) {
            var low = n - (n % 100);
            return Math.round(low);     
        }

        return Math.round(n/100) * 100;
    }

您可以将您的 var (变量)分配给任何东西。

于 2019-03-21T02:22:28.970 回答
-1
 int value = 0;
 for (int i = 100; i <= Math.round(total); i = i + 100) {
      if (i < Math.round(total)) {
          value = i;
         }
 }

它将一直运行到总 == 到 i 并且 i 每次都会增加

于 2021-02-09T07:06:10.030 回答