Tôi đang cố gắng chuyển đổi số giờ từ vài giờ sang ngày, giờ và phút.Chuyển đổi số thập phân sang DD: HH: MM
Đây là những gì tôi có cho đến nay, chưa hoàn toàn ở đó. Tôi cần phải trừ số giờ từ những ngày từ phần giờ nếu điều đó có ý nghĩa?
/// <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;
}
}
SOLUTION:
/// <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;
}
}
Cảm ơn, công việc ít hơn rất nhiều so với giải pháp của tôi. – WraithNath
Tôi tự hỏi liệu có trường hợp chuyển đổi từ thập phân sang gấp đôi có thể gây ra vấn đề hay không - và bạn có thể kết thúc với ít phút hơn bạn mong đợi, nhưng chắc chắn là một cách tiếp cận đơn giản :) –
Tôi đã thấy! Bình luận đã bị xóa :-). Câu trả lời hay (tất cả chúng tôi đều trả lời cùng một lúc). Bạn vẫn có +1 của tôi. –