如何在java中取整数字



我需要知道如何对数字进行四舍五入,例如

1448 => 1450
1651 => 1660
22 => 30
1001=> 1010

那么我应该用什么函数来对像这样的数字进行舍入呢

我不确定是否有特殊函数,但如果你有数字n,那么你要找的数字是(n/10+1)*10,如果是n%10 != 0,否则是n+10

所以,如果你想实现自己的功能,它将是类似于:

int roundUpTens(int n){
if (n % 10 == 0){
return (n+10);
}
return (n/10+1)*10;
}

另一种方法是找到除以10的余数,并用它来找到四舍五入数:

int roundUpTens(int n){
if (n % 10 == 0){
return (n+10);
}
return (n+10 - n%10);
}

最新更新