Convert decimal values to hours and minutes

Sheonarayan
Posted by Sheonarayan under C# category on | Points: 40 | Views : 1101
In case your input value is more than 24 hours, http://www.dotnetfunda.com/codes/show/6410/converting-decimal-values-to-hours-in-csharp function may not work. So use below functions

 public static string ConvertDecimalToMinutes(decimal hours)
{
string[] splitHours = hours.ToString().Split('.');
if (splitHours.Length > 1)
{
double minutes = double.Parse(splitHours[1]);
TimeSpan tt = TimeSpan.FromHours(double.Parse("0." + minutes));
return splitHours[0] + ":" + tt.Minutes.ToString("00");
}
else
{
return hours.ToString();
}
}

Use it like
Response.Write(ConvertDecimalToMinutes(30.75m).ToString());

This will give output as "30.45".

Comments or Responses

Login to post response