以30分钟(最近的半小时)递增: [英] Round up in 30 min (nearest half-hour) increments:
问题描述
以30为增量舍入整数的最有效方式(在Java中)是什么。我的输出将是一个整数:
What is the most efficient way (in Java) to round an integer in 30 increments. My output will be an Integer:
这是我的代码,它以整数形式返回分钟。
Here is my code that returns minutes in integer.
long different = endTime.getTime() - startTime.getTime();
int idiff = TimeUnit.MILLISECONDS.toMinutes(different) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(different));
现在我要整理这个整数。例如:
Now I want to round up this integer. For example:
任意数字< = 30 =向上舍入到30:
Any number <= 30 = round up to 30:
31至60之间的任何数字=向上舍入至60
Any number between 31 to 60 = round up to 60
61至90之间的任何数字=向上舍入至90
Any number between 61 to 90 = round up to 90
91到120之间的任何数字=最多120个
Any number between 91 to 120 = round up to 120
121到150之间的任何数字=最多150个
Any number between 121 to 150 = round up to 150
推荐答案
加一个比你想要的组少一个,然后除以多个..所以30个做
Add one less than the group you want, then divide and multiple.. so for 30 do
((value + 29) / 30) * 30
这假设您希望0保持0,30保持30 ...
This assumes you want 0 to stay at 0, 30 to stay at 30...
这篇关于以30分钟(最近的半小时)递增:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!