.NET CF.NET 1.0Windows 2003.NET 1.1Windows 2000Windows XP.NET 2.0移动应用C# 2.0中级开发Visual StudioWindows.NETC#
如何将时间按指定分钟数四舍五入?






2.93/5 (11投票s)
这是一个计算四舍五入时间的算法公式...
引言
这是一个非常简单的代码,展示了如何在 C# .NET 中计算四舍五入时间...
Using the Code
您可以将以下代码写入您的 Windows application form_load
方法并进行测试...
// Sample time: 01:07:00
TimeSpan t = new TimeSpan(1, 7, 0);
// The round number, here is a quarter...
int Round = 15;
// Count of round number in this total minutes...
double CountRound = (t.TotalMinutes / Round);
// The main formula to calculate round time...
int Min = (int)Math.Truncate(CountRound + 0.5) * Round;
// Now show the result...
TimeSpan tRes = new TimeSpan(0, Min, 0);
MessageBox.Show(tRes.ToString());
您可以将四舍五入的数字从 15 更改为您想要四舍五入的任何数字,例如 5 或 10 或...
祝您好运!
历史
- 2007 年 1 月 23 日:初始发布