Im пытается конвертировать числовое число часов в сутки, часы и минуты.Преобразование десятичных часов в ДД: HH: MM
Это то, что у меня есть до сих пор, его еще нет. Мне нужно вычесть количество часов из дней с части часов, если это имеет смысл?
/// <summary>
/// Converts from a decimal value to DD:HH:MM
/// </summary>
/// <param name="dHours">The total number of hours</param>
/// <returns>DD:HH:MM string</returns>
public static string ConvertFromDecimalToDDHHMM(decimal dHours)
{
try
{
decimal hours = Math.Floor(dHours); //take integral part
decimal minutes = (dHours - hours) * 60.0M; //multiply fractional part with 60
int D = (int)Math.Floor(dHours/24);
int H = (int)Math.Floor(hours);
int M = (int)Math.Floor(minutes);
//int S = (int)Math.Floor(seconds); //add if you want seconds
string timeFormat = String.Format("{0:00}:{1:00}:{2:00}", D, H, M);
return timeFormat;
}
catch (Exception)
{
throw;
}
}
РЕШЕНИЕ:
/// <summary>
/// Converts from a decimal value to DD:HH:MM
/// </summary>
/// <param name="dHours">The total number of hours</param>
/// <returns>DD:HH:MM string</returns>
public static string ConvertFromDecimalToDDHHMM(decimal dHours)
{
try
{
decimal hours = Math.Floor(dHours); //take integral part
decimal minutes = (dHours - hours) * 60.0M; //multiply fractional part with 60
int D = (int)Math.Floor(dHours/24);
int H = (int)Math.Floor(hours - (D * 24));
int M = (int)Math.Floor(minutes);
//int S = (int)Math.Floor(seconds); //add if you want seconds
string timeFormat = String.Format("{0:00}:{1:00}:{2:00}", D, H, M);
return timeFormat;
}
catch (Exception)
{
throw;
}
}
Спасибо, намного меньше работы, чем мое решение. – WraithNath
Интересно, возникают ли ситуации, когда преобразование из десятичного в двойное может вызвать проблемы - кажется, что может быть (и вы можете закончиться тем меньше, чем вы ожидаете, например), но это, безусловно, простой подход :) –
Я видел! Комментарий удален :-). Хороший ответ (мы все ответили одновременно). У тебя все еще есть +1. –