C#DateTime:缩放到时间 [英] C# DateTime: Scaling Double to Time

查看:138
本文介绍了C#DateTime:缩放到时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从有限范围返回步骤大小的功能。所以,如果范围是{} 1,2,3,4,5,6..10,我想5个步骤,它会返回2的步长这并不复杂。

I have a function that returns a step size from a bounded range. So if the range is {1,2,3,4,5,6..10} and I want 5 steps it would return a step size of 2. This isn't that complicated.

如果我有{.1 .2,.3 .4 .... 1},我想7步的步长为0.14285。那么我想0.14285转换成最接近的相关时间测量。在这种情况下,0.14285表示小数一天。例如,整数1将是一个整天和0.25将是6小时。

If I have {.1,.2,.3,.4....1} and I want 7 steps the step size is 0.14285. I then want to convert 0.14285 to the nearest relevant time measurement. In this case .14285 represents a fractional day. For example, the integer 1 would represent a whole day and .25 would represent 6 hours.

.14285 = 12,342.24 seconds = 205.704 Minutes = 3.42 Hours ~= 4 hours.



然后我想捕捉0.14285(4 * 60 * 60 = 14,400 /(24 * 60 * 60))= 0.16666这是4个小时的十进制等效值。

I then want to snap .14285 to (4*60*60 = 14,400 / (24*60*60)) = .16666 which is the decimal equivalent of 4 hours.

我或多或少都有数学想通了,但我想知道如果有一个?这样使用的DateTime东西更简单的方法

I more or less have the math figured out, but I'm wondering if there is an easier way of doing this using DateTime stuff?

推荐答案

我不建议你让你的问题更清晰;我花了一段时间才能意识到你指到的

I do recommend that you make your question clearer; it took me a while to realize that the 0.14285 you were referring to represented a fraction of a day.

您可以使用这样的:

 // {03:25:42.24}
TimeSpan unSnapped = TimeSpan.FromDays(0.14285);

 // {04:00:00} : Round up the hours and construct a TimeSpan from it
TimeSpan snapped = TimeSpan.FromHours(Math.Ceiling(unSnapped.TotalHours));

// 0.166666666
double snappedFractionOfADay = snapped.TotalDays;

您可以检查上的 TotalXXX 属性时间跨度让你提,如果需要其他信息。

You can inspect the TotalXXX properties on the time-spans to get the other information you mention, if needed.

这篇关于C#DateTime:缩放到时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆