C# String Format for hours and minutes from decimal

风流意气都作罢 提交于 2019-11-30 07:56:50

问题


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


回答1:


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.




回答2:


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;
}



回答3:


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}";


来源:https://stackoverflow.com/questions/5852305/c-sharp-string-format-for-hours-and-minutes-from-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!