how to perform division in timespan [duplicate]

安稳与你 提交于 2019-12-03 10:46:33

The simplest approach is probably just to take their lengths in ticks, and divide those. For example:

long ticks1 = tsp1.Ticks;
long ticks2 = tsp2.Ticks;

long remainder;
long count = Math.DivRem(ticks1, ticks2, out remainder);

TimeSpan remainderSpan = TimeSpan.FromTicks(remainder);

Console.WriteLine("tsp1/tsp2 = {0}, remainder {1}", count, remainderSpan);
Marcello Faga

a div b:

double adivb = (double)a.Ticks/b.Ticks;

edited:

i found another post on th same topic

How can I achieve a modulus operation with System.TimeSpan values, without looping?

An int will hold enough seconds for ~64 years, so as long as you stay well below that:

int count = (int) (tsp1.t.TotalSeconds / tsp2.t.TotalSeconds);
double remainder = tsp1.t.TotalSeconds - (count * tsp2.t.TotalSeconds);

And maybe convert the remainder to int as well.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!