I want to get the ratio of one TimeSpan against another TimeSpan (Basically the progress of a playing video from it's total time). My current methods is to get the milliseconds of the two TimeSpan objects and divide one against the other. Something like:
int durationInMilliseconds = totalTimeSpan.Milliseconds;
int progressInMilliseconds = progressTimeSpan.Milliseconds;
Double progressRatio = progressInMilliseconds / durationInMilliseconds;
Is there a more direct route? It's a simple problem and i'm just curious if there is a super elegant way to solve it.
Cheers all James
double progressRatio = progressTimeSpan.Ticks / (double)totalTimeSpan.Ticks;
You must cast one to a double, otherwise C# will do integer division. Ticks is better than the TotalMilliseconds because that is how it is stored and avoids any conversion.
You should use either Ticks or TotalMilliseconds, depending on the required precision. Milliseconds is the number of milliseconds past the current second.
As for a better solution, it doesn't get simpler than a division so your current solution is fine (minus the bug).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With