简体   繁体   English

如何向上舍入到指定数字的最接近倍数?

[英]How can I round up to the nearest multiple of the specified number?

I've looked at many questions regarding rounding up to the nearest multiple of a number, but I can't understand their methods well enough to adopt them for rounding up to 45 or they use language specific methods of other languages. 我已经查看了很多关于舍入到最接近的数字的倍数的问题,但是我无法理解它们的方法以便将它们用于四舍五入或者使用其他语言的语言特定方法。

Here is a bit more detailed explanation if the above doesn't make much sense yet: 如果以上内容没有多大意义,这里有一个更详细的解释:

Example input: 输入示例:

int num1 = 67;
int num2 = 43;

Expected results: 预期成绩:

>>round(num1) = 90
>>round(num2) = 45

It's enough to add the missing mod45 : 这足以添加缺少的mod45

int upperround45(int i) {
    int temp = i%45;
    //For the algorithm we wish the rest to be how much more than last multiple of 45.
    if (temp < 0 ) 
        temp = 45 + temp;
    if (temp == 0) {
        return i;
    } else {
        return i + 45 - temp;
    }
}

EDIT: 编辑:

In general: 一般来说:

int upperround(int num, int base) {
    int temp = num%base;
    if (temp < 0 ) 
        temp = base + temp;
    if (temp == 0) 
        return num;
    return num + base - temp;

Since people have such trouble with rounding to multiple of an integer number, whether rounding up/down/nearest, here are very simple methods for doing so: 由于人们在舍入到整数的倍数时会出现这样的问题,无论是向上/向下/最近舍入,这里都是非常简单的方法:

public static int roundDown(int value, int multiplier) {
    return value / multiplier * multiplier;
}
public static int roundHalfUp(int value, int multiplier) {
    return (value + multiplier / 2) / multiplier * multiplier;
}
public static int roundUp(int value, int multiplier) {
    return (value + multiplier - 1) / multiplier * multiplier;
}

Test 测试

Value Down Half   Up
    0    0    0    0
    1    0    0    4
    2    0    4    4
    3    0    4    4
    4    4    4    4
Value Down Half   Up
    0    0    0    0
    1    0    0    5
    2    0    0    5
    3    0    5    5
    4    0    5    5
    5    5    5    5

Negative Numbers 负数

They don't work right for negative numbers. 它们不能用于负数。 "Round down" usually means "towards zero", unlike Math.floor() which rounds towards negative infinity. “向下Math.floor() ”通常意味着“朝向零”,不像Math.floor()向负无穷大方向转。

Here are versions that can handle negative values. 以下是可以处理负值的版本。 These are consistent with the RoundingMode options of similar names used by BigDecimal . 这些与BigDecimal使用的类似名称的RoundingMode选项一致。

public static int roundDown(int value, int multiplier) {
    if (multiplier <= 0) throw new IllegalArgumentException();
    return value / multiplier * multiplier;
}
public static int roundHalfUp(int value, int multiplier) {
    if (multiplier <= 0) throw new IllegalArgumentException();
    return (value + (value < 0 ? multiplier / -2 : multiplier / 2)) / multiplier * multiplier;
}
public static int roundUp(int value, int multiplier) {
    if (multiplier <= 0) throw new IllegalArgumentException();
    return (value + (value < 0 ? 1 - multiplier : multiplier - 1)) / multiplier * multiplier;
}

Test 测试

Value Down Half   Up
   -4   -4   -4   -4
   -3    0   -4   -4
   -2    0   -4   -4
   -1    0    0   -4
    0    0    0    0
    1    0    0    4
    2    0    4    4
    3    0    4    4
    4    4    4    4
Value Down Half   Up
   -5   -5   -5   -5
   -4    0   -5   -5
   -3    0   -5   -5
   -2    0    0   -5
   -1    0    0   -5
    0    0    0    0
    1    0    0    5
    2    0    0    5
    3    0    5    5
    4    0    5    5
    5    5    5    5

The easiest way I can think of is the following 我能想到的最简单的方法如下

public static int round(int num) {
    return num - num % 45 + (num%45==0? 0 : 45);
}

Tested 经测试

int num1 = 67;
int num2 = 43;
int num3 = 90;

System.out.println(num1 - num1 % 45 + (num1%45==0? 0 : 45)); // 90
System.out.println(num2 - num2 % 45 + (num2%45==0? 0 : 45)); // 45
System.out.println(num3 - num3 % 45 + (num3%45==0? 0 : 45)); // 90

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM