Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How do I convert a TimeSpan value to a double?

How do I convert a TimeSpan value to a double in C#?

I mean I have this -08:15:00 and I want a double -08.15.

like image 414
user2037696 Avatar asked Apr 27 '13 13:04

user2037696


2 Answers

Despite how ambiguous this question is, for future reference, Umar was probably the closest to answering the question, as it was displayed in the title.

To get a double from a TimeSpan object, you need to pick the most significant measurement, and get the total. As the Total<x> property, will return the appropriate full value, and the fractions of that value as a decimal.

So, if you want 8:15:00, to a double - and the "8" represents Hours, then you'll want TimeSpan.TotalHours which will result in a value of 8.25.

If the "8" represents Minutes, then again, you'll use the appropriate property TimeSpan.TotalMinutes for the same result, and so on.

like image 193
Digital_Utopia Avatar answered Sep 23 '22 00:09

Digital_Utopia


You could use TimeSpan.TotalMinutes (gets the value of the current TimeSpan structure expressed in whole and fractional minutes) or other similar properties.

like image 30
Umar Farooq Khawaja Avatar answered Sep 23 '22 00:09

Umar Farooq Khawaja