Can you round a .NET TimeSpan object?

后端 未结 10 921
渐次进展
渐次进展 2020-12-03 03:01

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 TimeSp

10条回答
  •  爱一瞬间的悲伤
    2020-12-03 03:31

    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.

    TimeSpan t1 = new TimeSpan(2345678);
    Console.WriteLine(t1);
    TimeSpan t2 = new TimeSpan(t1.Ticks - (t1.Ticks % 100000));
    Console.WriteLine(t2);
    

    Gives:

    00:00:00.2345678
    00:00:00.2300000
    

提交回复
热议问题