将int数舍入到另一个int数

use*_*674 1 java int rounding

while(potatosconeflour <= c1) {
    potatosconeflour = potatosconeflour + potatosconeflour;
}
Run Code Online (Sandbox Code Playgroud)

我使用了一个while循环,它在输入数字24后不起作用.我试图将一个int数舍入到另一个int数.例如,我想将任何数字四舍五入为8的倍数.

例如:舍入1到8,13到16,23到24

Mur*_*nik 5

我将源数除以数字以使其四舍五入(注意:将其转换为a,double因此不要使用整数除法!)使用Math.ceil向上舍入结果,然后将其乘以相同的数字:

public static int roundToMultiple(int toRound, int roundBy) {
    return roundBy * (int) Math.ceil((double)toRound / roundBy);
}
Run Code Online (Sandbox Code Playgroud)