您可以舍入 .NET TimeSpan 对象吗?
您可以舍入 .NET TimeSpan
对象吗?
我的 Timespan
值为:00:00:00.6193789
有没有一种简单的方法可以将其保留为 TimeSpan
对象,但将其舍入为类似
00:00:00.62?
Can you round a .NET TimeSpan
object?
I have a Timespan
value of: 00:00:00.6193789
Is there a simple way to keep it a TimeSpan
object but round it to something like
00:00:00.62?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
抱歉,伙计们,但是这个问题和迄今为止流行的答案都是错误的:-)
这个问题是错误的,因为廷德尔要求一种方法圆,但显示了截断的示例。
Will Dean 的答案是错误的,因为它还解决了截断问题,而不是舍入问题。 (我想有人可能会认为答案对于两个问题之一是正确的,但让我们暂时把哲学放在一边......)
这是一种简单的四舍五入技巧 :
根据示例代码中的输入值和位数,这就是输出:
将精度从 2 位更改为 5 位,然后得到此结果:
甚至将其更改为 0 以获得此结果:
最后,如果您想要对输出进行更多控制,添加一些格式。 下面是一个示例,显示您可以将精度与显示的位数分开。 精度再次设置为 2,但显示 3 位数字,如格式控制字符串的最后一个参数中所指定:
2010.01.06 更新:开箱即用的解决方案
如果您正在寻找想法,上述材料很有用; 从那以后,我有时间为那些寻找即用代码的人实现一个打包的解决方案。
请注意,这是未注释的代码。 带有 XML-doc-comments 的完整注释版本将于本季度末在我的开源库中提供。 虽然我犹豫是否像这样“原始”发布它,但我认为它仍然对感兴趣的读者有一些好处。
此代码改进了我上面的代码,尽管它四舍五入,但仍然显示 7 个位置,并用零。 此完成版本四舍五入并修剪为指定的位数。
这是一个示例调用:
这是完整的 RoundedTimeSpan.cs 文件:
2010.02.01 更新:打包解决方案现已可用
我昨天刚刚发布了我的开源库的新版本,比预期的要早,包括我上面描述的 RoundedTimeSpan。 代码位于此处; 对于 API,请从此处开始,然后导航至
RoundedTimeSpan
>CleanCode.Data 命名空间。 CleanCode.DLL 库包含上面显示的代码,但以成品包形式提供。 请注意,自从我在 2010 年 1 月 6 日发布以来,我对上面的ToString(int)
方法做了一些改进。Sorry, guys, but both the question and the popular answer so far are wrong :-)
The question is wrong because Tyndall asks for a way to round but shows an example of truncation.
Will Dean's answer is wrong because it also addresses truncation rather than rounding. (I suppose one could argue the answer is right for one of the two questions, but let's leave philosophy aside for the moment...)
Here is a simple technique for rounding:
With the input value and number of digits in the sample code, this is the output:
Change the precision from 2 digits to 5 digits and get this instead:
And even change it to 0 to get this result:
Finally, if you want just a bit more control over the output, add some formatting. Here is one example, showing that you can separate the precision from the number of displayed digits. The precision is again set to 2 but 3 digits are displayed, as specified in the last argument of the formatting control string:
2010.01.06 UPDATE: An Out-of-the-box Solution
The above material is useful if you are looking for ideas; I have since had time to implement a packaged solution for those looking for ready-to-use code.
Note that this is uncommented code. The fully commented version with XML-doc-comments will be available in my open source library by the end of the quarter. Though I hesitated to post it "raw" like this, I figure that it could still be of some benefit to interested readers.
This code improves upon my code above which, though it rounded, still showed 7 places, padded with zeroes. This finished version rounds and trims to the specified number of digits.
Here is a sample invocation:
And here is the complete RoundedTimeSpan.cs file:
2010.02.01 UPDATE: Packaged solution now available
I just released a new version of my open-source libraries yesterday, sooner than anticipated, including the RoundedTimeSpan I described above. Code is here; for the API start here then navigate to
RoundedTimeSpan
under theCleanCode.Data
namespace. The CleanCode.DLL library includes the code shown above but provides it in a finished package. Note that I have made a slight improvement in theToString(int)
method above since I posted it on 2010.01.06.如果要四舍五入到整秒,则最简单的一行:
四舍五入到最多 3 位数字(例如十分之一、百分之一秒或毫秒:
对于超过 3 位数字,它稍微复杂一些 - 但仍然是单行。这也可以用于 3 个或更少的数字 - 它是上面显示的版本的替代品:
如果你想要一个字符串(根据评论,这种技术最多只能工作到) 7 位数字):
对于一天中的时间,如小时和分钟,四舍五入:
如果您有 DateTime,并且想要提取一天中的时间四舍五入:
显示四舍五入的 24 小时time:
要显示当前文化中的时间:
学分:
cc1960 的答案显示了 FromSeconds 的使用,但他四舍五入到整秒。
Ed 的答案建议使用格式字符串,并包含指向的链接 。
Chris Marisic 展示了如何将
ToShortTimeString
应用于 TimeSpan(首先转换为日期时间
)。要舍入为其他单位的倍数,例如 1/30 秒:
要使用其中之一舍入为 1/30 秒的倍数:
在调试器中查看结果:
Simplest one-liner if you are rounding to whole seconds:
To round to up to 3 digits (e.g. tenths, hundredths of second, or milliseconds:
For more than 3 digits, its slightly more complex - but still a one-liner. This can also be used for 3 or less digits - it is a replacement for the version shown above:
If you want a string (according to a comment, this technique only works up to 7 digits):
For time of day, as hours and minutes, rounded:
If you have a DateTime, and want to extract time of day rounded:
To display rounded 24 hour time:
To display time of day in current culture:
Credits:
cc1960's answer shows use of FromSeconds, but he rounded to whole seconds. My answer generalizes to specified number of digits.
Ed's answer suggests using a format string, and includes a link to the formatting document.
Chris Marisic shows how to apply
ToShortTimeString
to a TimeSpan (by first converting to aDateTime
).To round to multiple of some other unit, such as 1/30 second:
To use one of these to round to multiples of 1/30 second:
Results viewed in debugger:
TimeSpan 只不过是“Ticks”成员的包装。 从另一个 TimeSpan 的 Ticks 的舍入版本创建一个新的 TimeSpan 非常容易。
给出:
TimeSpan is little more than a wrapper around the 'Ticks' member. It's pretty easy to create a new TimeSpan from a rounded version of another TimeSpan's Ticks.
Gives:
鉴于一些关于舍入到秒的评论,我认为舍入到任何 TimeSpan 会很好:
考虑到处理小数单位时舍入到刻度的潜在不准确性(根据 @ToolmakerSteve),我添加了一个小数舍入选项,以便当您需要更高的值时精度并四舍五入到计算机小数秒:
Given some of the comments about rounding to seconds, I thought rounding to any TimeSpan would be nice:
Given the potential inaccuracies rounding to ticks when dealing with fractional units (per @ToolmakerSteve), I am adding a fractional rounding option for when you need higher accuracy and are rounded to a computer fractional seconds:
我的解决方案:
My solution:
不确定 TimeSpan,但您可以查看有关 DateTimes 的这篇文章:
http://mikeinmadison.wordpress.com/2008/03/12/datetimeround/< /a>
Not sure about TimeSpan, but you might check this post on DateTimes:
http://mikeinmadison.wordpress.com/2008/03/12/datetimeround/
将毫秒四舍五入到最接近的秒的另一种方法。
Yet another way to round milliseconds to the nearest second.
这是一个很好的扩展方法:
这里有一些示例:
Here is a nice Extention-Method:
And here are some Examples:
如果您需要使用
DateTime
,但仍想对时间进行舍入,则可以使用扩展方法。 就我而言,我想四舍五入到分钟。An extension method if you need to work with
DateTime
instead, but still want to round the time. In my case, I wanted to round to the minute.