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
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
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.
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;
}
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