我在.NET中划分两个双精度并使用结果对象通过调用(dtStart是预定义的)从开始日期计算结束日期:
var dValue = 1500.0/8400.0;
var dtEnd = dtStart.AddDays(dValue);
Run Code Online (Sandbox Code Playgroud)
在检查dtEnd之后,我发现结果只精确到最接近的毫秒.在查找之后,我发现.AddMilliseconds等全部围绕数字而TimeSpan.FromDays做了类似的事情.我想知道是否有理由为什么这个舍入已经完成,因为看起来这里获得正确值的唯一方法是使用.AddTicks?
供参考.AddDays调用(其中MillisPerDay = 86400000)
public DateTime AddDays(double value)
{
return Add(value, MillisPerDay);
}
Run Code Online (Sandbox Code Playgroud)
哪个叫
private DateTime Add(double value, int scale)
{
long millis = (long)(value * scale + (value >= 0? 0.5: -0.5));
if (millis <= -MaxMillis || millis >= MaxMillis)
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_AddValue"));
return AddTicks(millis * TicksPerMillisecond);
}
Run Code Online (Sandbox Code Playgroud)
编辑:经过深思熟虑,我现在意识到我的答案的第一个版本是错误的。
以下是微软源代码中的注释:
// Returns the DateTime resulting from adding a fractional number of
// xxxxs to this DateTime. The result is computed by rounding the
// fractional number of xxxxs given by value to the nearest
// millisecond, and adding that interval to this DateTime. The
// value argument is permitted to be negative.
Run Code Online (Sandbox Code Playgroud)
这些注释出现在五种不同的 AddXxxxs(double value) 方法中,其中 Xxxx = 天、小时、毫秒、分钟和秒。
请注意,这仅适用于接受浮点值的方法。(人们可能会质疑在日期计算中使用浮点值是否是一个好主意 - 但这是另一天的主题。)
现在,正如OP正确指出的那样,这五个方法都调用此方法:
// Returns the DateTime resulting from adding a fractional number of
// time units to this DateTime.
private DateTime Add(double value, int scale) {
long millis = (long)(value * scale + (value >= 0? 0.5: -0.5));
if (millis <= -MaxMillis || millis >= MaxMillis)
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_AddValue"));
return AddTicks(millis * TicksPerMillisecond);
}
Run Code Online (Sandbox Code Playgroud)
因此,所做的就是将添加到 DateTime 的值在添加之前四舍五入到最接近的毫秒数。但不是结果 - 只有被添加(或减去)的值。
这实际上有记录,例如http://msdn.microsoft.com/en-us/library/system.datetime.adddays%28v=vs.110%29.aspx “值参数四舍五入到最接近的毫秒。”
我不知道为什么这样做。也许程序员认为,如果您使用浮点值,您应该意识到您的值通常不完全准确。或者也许他们想在某种程度上模拟 Java 风格的时间,这是基于毫秒的。
| 归档时间: |
|
| 查看次数: |
439 次 |
| 最近记录: |