将数字四舍五入为最接近的5的倍数

有谁知道如何将数字四舍五入到最接近的5的倍数? 我找到了一个算法将它四舍五入到最近的10的倍数,但我找不到这个。

这样做十点。

double number = Math.round((len + 5)/ 10.0) * 10.0; 

 int roundUp(int n) { return (n + 4) / 5 * 5; } 

注 – YankeeWhiskey的答案是四舍五入到最接近的倍数,这是四舍五入。 如果您需要修改负数,则需要进行修改。 请注意,整数除法后跟相同数字的整数乘法是向下舍入的方法。

舍入到最接近的任何值

 int round(double i, int v){ return Math.round(i/v) * v; } 

您还可以使用Math.floor()Math.ceil()替换Math.round() ,使其始终向下舍入或始终向上舍入。

感谢Amir,我想我拥有它

 double round( double num, int multipleOf) { return Math.floor((num + multipleOf/2) / multipleOf) * multipleOf; } 

这是我运行的代码

 class Round { public static void main(String[] args){ System.out.println("3.5 round to 5: " + Round.round(3.5, 5)); System.out.println("12 round to 6: " + Round.round(12, 6)); System.out.println("11 round to 7: "+ Round.round(11, 7)); System.out.println("5 round to 2: " + Round.round(5, 2)); System.out.println("6.2 round to 2: " + Round.round(6.2, 2)); } public static double round(double num, int multipleOf) { return Math.floor((num + (double)multipleOf / 2) / multipleOf) * multipleOf; } } 

这是输出

 3.5 round to 5: 5.0 12 round to 6: 12.0 11 round to 7: 14.0 5 round to 2: 6.0 6.2 round to 2: 6.0 
 int roundUp(int num) { return (int) (Math.ceil(num / 5d) * 5); } 
 int roundUp(int num) { return ((num / 5) + (num % 5 > 0 ? 1 : 0)) * 5; } 
 int round(int num) { int temp = num%5; if (temp<3) return num-temp; else return num+5-temp; } 

有些人说的是这样的

 int n = [some number] int rounded = (n + 5) / 5 * 5; 

这将围绕,例如,5到10,以及6,7,8和9(全部到10)。 你不希望5轮到10。 当处理只是整数时,你想要添加4到n而不是5。所以取这个代码并用4替换5:

 int n = [some number] int rounded = (n + 4) / 5 * 5; 

当然,在处理双打时,只需输入类似4.99999的内容,或者如果你想要考虑所有情况(如果你可能正在处理更精确的双打),添加一个条件声明:

 int n = [some number] int rounded = n % 5 == 0 ? n : (n + 4) / 5 * 5; 

递归:

 public static int round(int n){ return (n%5==0) ? n : round(++n); } 

只需将您的数字作为double传递给此函数,它将返回您将十进制值四舍五入到最接近的值5;

如果4.25,输出4.25

如果4.20,输出4.20

如果4.24,输出4.20

如果4.26,输出4.30

如果你想要舍入到2位小数,那么使用

 DecimalFormat df = new DecimalFormat("#.##"); roundToMultipleOfFive(Double.valueOf(df.format(number))); 

如果最多3个地方,新的DecimalFormat(“#。###”)

如果最多n个地方,新的DecimalFormat(“#。 nTimes# ”)

  public double roundToMultipleOfFive(double x) { x=input.nextDouble(); String str=String.valueOf(x); int pos=0; for(int i=0;i 

这是我用于舍入到数字的倍数的内容:

 private int roundToMultipleOf(int current, int multipleOf, Direction direction){ if (current % multipleOf == 0){ return ((current / multipleOf) + (direction == Direction.UP ? 1 : -1)) * multipleOf; } return (direction == Direction.UP ? (int) Math.ceil((double) current / multipleOf) : (direction == Direction.DOWN ? (int) Math.floor((double) current / multipleOf) : current)) * multipleOf; } 

变量current是你要舍入的数字, multipleOf是你想要的倍数(即舍入到最接近的20,最接近的10等),而direction是我向上或向下舍入的枚举。

祝你好运!

将数字四舍五入为最接近的5的另一种方法或逻辑

 double num = 18.0; if (num % 5 == 0) System.out.println("No need to roundoff"); else if (num % 5 < 2.5) num = num - num % 5; else num = num + (5 - num % 5); System.out.println("Rounding up to nearest 5------" + num); 

输出:

 Rounding up to nearest 5------20.0 

我已经创建了一个方法,可以将一个数字转换为最接近的数字,也许它会对某人有所帮助,因为我在这里看到很多方法并且它对我没有用但是这个方法做了:

 /** * The method is rounding a number per the number and the nearest that will be passed in. * If the nearest is 5 - (63->65) | 10 - (124->120). * @param num - The number to round * @param nearest - The nearest number to round to (If the nearest is 5 -> (0 - 2.49 will round down) || (2.5-4.99 will round up)) * @return Double - The rounded number */ private Double round (double num, int nearest) { if (num % nearest >= nearest / 2) { num = num + ((num % nearest - nearest) * -1); } else if (num % nearest < nearest / 2) { num = num - (num % nearest); } return num; } 

如果您只需要对整数进行舍入,则可以使用此函数:

 public static long roundTo(long value, long roundTo) { if (roundTo <= 0) { throw new IllegalArgumentException("Parameter 'roundTo' must be larger than 0"); } long remainder = value % roundTo; if (Math.abs(remainder) < (roundTo / 2d)) { return value - remainder; } else { if (value > 0) { return value + (roundTo - Math.abs(remainder)); } else { return value - (roundTo - Math.abs(remainder)); } } } 

它的优点是它使用整数算术,甚至可以用于大型长数,浮点除法会导致问题。

将给定数字舍入到最接近的5的倍数。

 public static int round(int n) while (n % 5 != 0) n++; return n; } 
 int roundUp(int n, int multipleOf) { int a = (n / multipleOf) * multipleOf; int b = a + multipleOf; return (n - a > b - n)? b : a; } 

来源: https : //www.geeksforgeeks.org/round-the-given-number-to-nearest-multiple-of-10/

 if (n % 5 == 1){ n -= 1; } else if (n % 5 == 2) { n -= 2; } else if (n % 5 == 3) { n += 2; } else if (n % 5 == 4) { n += 1; } 

码:

    公共课MyMath
     {
         public static void main(String [] args){
             runTests();
         }
         public static double myFloor(double num,double multipleOf){
             return(Math.floor(num / multipleOf)* multipleOf);
         }
         public static double myCeil(double num,double multipleOf){
             return(Math.ceil(num / multipleOf)* multipleOf);
         }

         private static void runTests(){
             System.out.println(“myFloor(57.3,0.1):”+ myFloor(57.3,0.1));
             System.out.println(“myCeil(57.3,0.1):”+ myCeil(57.3,0.1));
            的System.out.println( “”);
             System.out.println(“myFloor(57.3,1.0):”+ myFloor(57.3,1.0));
             System.out.println(“myCeil(57.3,1.0):”+ myCeil(57.3,1.0));
            的System.out.println( “”);
             System.out.println(“myFloor(57.3,5.0):”+ myFloor(57.3,5.0));
             System.out.println(“myCeil(57.3,5.0):”+ myCeil(57.3,5.0));
            的System.out.println( “”);
             System.out.println(“myFloor(57.3,10.0):”+ myFloor(57.3,10.0));
             System.out.println(“myCeil(57.3,10.0):”+ myCeil(57.3,10.0));
         }
     }

输出: myCeil中有一个错误,也是0.1的倍数……不明白为什么。

     myFloor(57.3,0.1):57.2
     myCeil(57.3,0.1):57.300000000000004

     myFloor(57.3,1.0):57.0
     myCeil(57.3,1.0):58.0

     myFloor(57.3,5.0):55.0
     myCeil(57.3,5.0):60.0

     myFloor(57.3,10.0):50.0
     myCeil(57.3,10.0):60.0