优秀 – 舍入时间精确到15分钟

我有一个计算几个TIMES的差异的公式,并乘以24获得总和的十进制值:

D10=(C9-D9)*24 

我想这样做特别的四舍五入:

如果D9 is 1pmC9 is 2:08 pm ,我想D10是1.25

换句话说

 1:53 until 2:07 would be counted as 2 2:08 until 2:22 would be 2:15 2:23 through 2:37 would be 2:30 etc 

有谁知道如何做这种特殊的四舍五入?

正如道格所言:

 i need the accrued time from 1 PM to 2:08 PM should round up to 1.25 hours whereas 1 PM to 2 PM would round down to 1 hour, in other words to the nearest quarter hour. 

以下是将按照您的要求划分差异的公式:

 =(TRUNC((D9+VALUE("00:07"))*96)-TRUNC((C9+VALUE("00:07"))*96))*VALUE("00:15")*24 

如果你想要几小时的时间差,但四舍五入到最接近的一刻钟,那么也许你需要的东西是这样的:

 =MROUND((C9-D9)*24,0.25) 

Excelfunction版本:

根据评论的要求,这里是一个只有excel函数的版本:

环绕到下一个15分钟的边界:

= TIME(HOUR(A1),15 * CEILING(MINUTE(A1)/ 15,1),0)

  • NB根据原始问题用(t2-t1)代替A1

圆到最近的15分钟边界:

= TIME(HOUR(A1),15 * ROUND(MINUTE(A1)/ 15,0),0)

顺便说一句:转换为string做date操作应该避免性能的原因,也可能也是地缘的原因。 也许这些东西的关键是你的关心。


VBA版本

你可以用div和mod函数来做。 不知道如果你想在VBA或Excelmacros,但这里的方法。 现在我的面前没有优秀的performance,所以这里…

(如果您需要重复使用,请在vba中进行)

转换为分数

d = floor(n / 0.25)

余数= n mod 0.25

x = 0.25 * round(余数/ 0.25)

答案=(d * 0.25)+ x

这应该是密切的。

…只是想到excel债券“打勾”到十进制function(又是什么)。 这可能是更好的方法。

在vba中进行编码…只进行基本testing:

 Public Function f(n As Double) As Double Dim d As Double, r As Double, x As Double d = Int(n / 0.25) r = n - (d * 0.25) ' strange... couldn't use mod function with decimal? x = 0.25 * Round(r / 0.25) f = (d * 0.25) + x End Function 

用法:

(在一个单元格中)

= F(C9-D9)

这是另一种forms的select

 =(ROUND(+D9*96,0)-ROUND(+C9*96,0))/4 

将每次串行转换为1/4小时(* 96),循环,添加结果,并缩放到小时(* 4)