Converting decimal values to hours in C#

Sheonarayan
Posted by Sheonarayan under C# category on | Points: 40 | Views : 1740
To convert decimal values to hours, use below function.
public static string ConvertDecimalToHours(decimal hours)
{
TimeSpan tt = TimeSpan.FromHours(Decimal.ToDouble(hours));
return tt.Hours.ToString("00") + ":" + tt.Minutes.ToString("00");
}

Use this function like
Response.Write(ConvertDecimalToHours(3.75m).ToString());

This will output result as "03:45".

If above function doesn't work (if your hours values are more than 24) then use http://www.dotnetfunda.com/codes/show/6411/convert-decimal-values-to-hours-and-minutes.

Thanks

Comments or Responses

Login to post response