C# – method to display float in datetime format

c++datetimefloating-point

I need a method in c# that makes me a string that look like a datetime out of a double/float.

for example

1.5 -> 01:30
2.8 -> 02:48
25.5 -> 25:30 (note: don't display as day, display full hours even its more than 24)

Best Solution

Try the following:

string ToTime(double hours)
{
    var span=TimeSpan.FromHours(hours);
    return string.Format("{0}:{1:00}:{2:00}",
        (int)span.TotalHours, span.Minutes, span.Seconds);
}

Or if you don't want seconds:

string.Format("{0}:{1:00}", (int)span.TotalHours, span.Minutes)