Преобразование десятичных часов в ДД:ЧЧ:ММ

Я пытаюсь преобразовать десятичное число часов в дни, часы и минуты.

Это то, что у меня есть до сих пор, это еще не совсем так. Мне нужно вычесть количество часов из дней из части часов, если это имеет смысл?

        /// <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;
        }
    }
14
задан WraithNath 14 March 2012 в 09:28
поделиться