以 30 为增量舍入整数的最有效方法(在 Java 中)是什么。我的输出将是一个整数:
这是我的代码,它以整数形式返回分钟。
long different = endTime.getTime() - startTime.getTime();
int idiff = TimeUnit.MILLISECONDS.toMinutes(different) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(different));
现在我想四舍五入这个整数。例如:
Any number <= 30 = round up to 30:
Any number between 31 to 60 = round up to 60
Any number between 61 to 90 = round up to 90
Any number between 91 to 120 = round up to 120
Any number between 121 to 150 = round up to 150
最佳答案
比你想要的组少加一个,然后除以倍数。所以 30 次
((value + 29) / 30) * 30
这假设您希望 0 保持在 0,30 保持在 30...
关于java - 以 30 分钟(最近的半小时)增量舍入 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27082097/