16

Is there a simple string format that will take a decimal representing hours and fractions of hours and show it as hours and minutes?

For example : 5.5 formatted to display 5 hrs 30 minutes.

I am happy to write the code myself, however would prefer to use existing functionality if it is available

3 Answers 3

29
decimal t = 5.5M;
Console.WriteLine(TimeSpan.FromHours((double)t).ToString());

That'll give you "05:30:00" which is pretty close. You could then format that to your desired result:

var ts = TimeSpan.FromHours((double)t);
Console.WriteLine("{0} hrs {1} minutes", ts.Hours, ts.Minutes);

Note that there's a potential for loss of accuracy in the conversion from decimal to double, but I don't think it would be noticeable on the minute scale. Someone with a Skeet-like understanding of the type system might be able to chime in here.

3
  • 1
    Nice, I didn't think of using TimeSpan (I probably should have though :) ). I added a little more formatting to hide the minutes if they were 0: string.Format("{0} hrs{1}", ts.Hours, ts.Minutes > 0 ? string.Format(" {0}", ts.Minutes) : "");
    – johnc
    May 2, 2011 at 0:09
  • Yep good idea. I've used TimeSpan like this in Reporting Services reports too, to get a human-readable time span. It's a great trick. May 2, 2011 at 0:13
  • 1
    The second example is to be avoided if you have timespan > 24 hours.
    – Matthieu
    Oct 17, 2017 at 17:43
2

If you have time that can be more than 24 hours, you have to consider that the TimeSpan class has a Days property, and the Hours property will cycle. Something like this will prevent any error from time with more than 24 hours:

 var tsExample = TimeSpan.FromHours(timeInDecimal);
 var result = $"{tsExample.Hours + (tsExample.Days * 24):00}:{tsExample.Minutes:00}";
1

I wrote a few small helper methods for this based on Matt's comment. Might be useful for someone to save you writing it yourself.

/// <summary>Converts a decimal e.g. 1.5 to 1 hour 30 minutes</summary>
/// <param name="duration">The time to convert as a double</param>
/// <returns>
///     Returns a string in format:
///     x hours x minutes
///     x hours (if there's no minutes)
///     x minutes (if there's no hours)
///     Will also pluralise the words if required e.g. 1 hour or 3 hours
/// </returns>
public String convertDecimalToHoursMinutes(double time)
{
    TimeSpan timespan = TimeSpan.FromHours(time);
    int hours = timespan.Hours;
    int mins = timespan.Minutes;

    // Convert to hours and minutes
    String hourString = (hours > 0) ? string.Format("{0} " + pluraliseTime(hours, "hour"), hours) : "";
    String minString = (mins > 0) ? string.Format("{0} " + pluraliseTime(mins, "minute"), mins) : "";

    // Add a space between the hours and minutes if necessary
    return (hours > 0 && mins > 0) ? hourString + " " + minString : hourString + minString;
}

/// <summary>Pluralise hour or minutes based on the amount of time</summary>
/// <param name="num">The number of hours or minutes</param>
/// <param name="word">The word to pluralise e.g. "hour" or "minute"</param>
/// <returns> Returns correct English pluralisation e.g. 3 hours, 1 minute, 0 minutes</returns>
public String pluraliseTime(int num, String word)
{
    return (num == 0 || num > 1) ? word + "s" : word;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.