Number of C # samples over a period of time

I would like to know a way to do this in C #

Let's say I have 2 times: TS1 is 3h and TS2 is 12h.

What is the fastest way to calculate how many times TS1 can go to TS2? In this case, the output will be 4.

if TS1 is 8 days and TS2 is 32 days, it will also return 4.

+3
source share
4 answers

Yes, use integer division. But the devil is in the details, be sure to use the built-in TimeSpan property to avoid problems with overflow and rounding:

 int periods = (int)(TS1.Ticks / TS2.Ticks);
+8
source

Integer division?

(int) TS1.TotalMilliseconds/(int) TS2.TotalMilliseconds;
+8
source

TotalMilliseconds . :

double times = TS2.TotalMilliseconds / TS1.TotalMilliseconds
+4

int count = (int) (ts2.TotalSeconds/ts1.TotalSeconds);

+2

Source: https://habr.com/ru/post/1765444/


All Articles