在C#中将TimeSpan舍入为100 ms [英] Rounding a TimeSpan to 100 ms in C#

查看:206
本文介绍了在C#中将TimeSpan舍入为100 ms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算一个计时器的刻度,我用它来发送一个自定义函数的自定义函数,该函数使用00h:00m:00s格式的文本更新标签。 000毫秒:



I'm counting ticks from a timer which I use to send a double to a custom function which updates a label with text in the format of 00h : 00m : 00s . 000 ms :

private string TimeRetrieval(double milliseconds)
{
    TimeSpan tmspan = TimeSpan.FromMilliseconds(milliseconds);

    string timelapse = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}",
    tmspan.Hours,
    tmspan.Minutes,
    tmspan.Seconds,
    tmspan.Milliseconds);


    return timelapse;
}





它有效,但我只想显示从小时到十分之一秒,如下所示:00: 00:00.0不是精确的毫秒。我用谷歌搜索和Binged搜索并寻找了几天应该是一个简单的答案。其他社区中接受的某些代码在编译之前会出错。我使用的是较旧的笔记本电脑,因此我使用C#2008 ee和.NET 4.0运行XP SP3。 4.0支持TimeSpans的自定义格式,所以我有点困惑为什么我在使用适用于其他人的代码时遇到问题。我已经阅读了.f参数,但它对我不起作用。我错过了参考吗?有人可以解释如何正确地圆到最接近的十分之一秒?如果有必要知道我正在制作一个自定义视频播放器,这将是播放标记参考。还有什么是timer.interval的正确值,以便以适当的速度计算1秒?谢谢你的帮助。



It works but what I only want to display from hours to tenths of a second, like this: 00:00:00.0 not exact milliseconds. I have Googled and Binged and search and sought for days for what should be a simple answer. Some code accepted in other communities gives errors prior to compilation. I am using an older laptop so I'm running XP SP3 with C# 2008 ee and .NET 4.0. 4.0 supports custom formatting of TimeSpans so I'm a bit confuzzled as to why I'm having issues using code which works for others. I've read up on the .f parameter but it doesn't work for me. Am I missing a reference ? Could someone please explain how to round properly to the nearest tenth of a second ? If it helps to know I am making a custom video player and this will be the playback marker reference. Also what is the proper value for timer.interval so that 1 second is counted at the proper speed ? I thank you for your help.

推荐答案

你提到ticks,但你的输入参数是一个名为'毫秒的双。因为每毫秒有10,000个刻度;十分之一秒将是100毫秒== 1毫秒的嘀嗒声,我想知道你是否真的在传递'滴答值'。我们在这里是同一页吗?



有精确,然后在.NET中有准确度:;在编码中需要仔细考虑这些术语:本文可能有助于您理解使用WinForms Timer Control ...或使用DateTime Struct或使用DateTime.Now可能缺少准确性: [ ^ ]。



如果您确实需要最高精度,在毫秒级别,我建议您切换到使用'StopWatch对象可能具有纳秒精度:[ ^ ]。



为了解决为什么我在使用适合其他人的代码时出现问题,我有点困惑。对于这类问题,如果您按原样显示代码的具体示例,并清楚地显示当前结果和预期结果,将会很有帮助。
You mention "ticks," but your input parameter is a double named 'milliseconds. Since there are 10,000 ticks per millisecond; a tenth of a second would be 100 milliescond == 1 millio ticks, I wonder if you are actually passing in the 'ticks value. Are we on the same page here ?

There's "precision," and then there's "accuracy:" in .NET; those terms need to be carefully considered in coding: this article may be useful in your understanding the possible lack of accuracy with using a WinForms Timer Control ... or using DateTime Struct, or using DateTime.Now: [^].

If you really need the best accuracy available, at the millisecond level, I suggest you switch to using the 'StopWatch object which has possible nanosecond precision: [^].

"I'm a bit confuzzled as to why I'm having issues using code which works for others." For this type of question, it is helpful if you shown a concrete example of your code as is, and clearly show the current result, and the expected result.


这篇关于在C#中将TimeSpan舍入为100 ms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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