C# 十进制字符串格式的小时和分钟

发布于 2024-11-04 14:59:49 字数 120 浏览 0 评论 0原文

是否有一种简单的字符串格式,可以采用代表小时和小时分数的小数并将其显示为小时和分钟?

例如:5.5 格式化为显示 5 小时 30 分钟。

我很高兴自己编写代码,但更愿意使用现有的功能(如果可用)

Is there a simple string format that will take a decimal representing hours and fractions of hours and show it as hours and minutes?

For example : 5.5 formatted to display 5 hrs 30 minutes.

I am happy to write the code myself, however would prefer to use existing functionality if it is available

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风挽心 2024-11-11 14:59:49
decimal t = 5.5M;
Console.WriteLine(TimeSpan.FromHours((double)t).ToString());

这会给你“05:30:00”,这非常接近。然后,您可以将其格式化为您想要的结果:

var ts = TimeSpan.FromHours((double)t);
Console.WriteLine("{0} hrs {1} minutes", ts.Hours, ts.Minutes);

请注意,从十进制转换为双精度可能会损失准确性,但我认为这在分钟刻度上不会很明显。对类型系统有类似飞碟般理解的人也许能够在这里插话。

decimal t = 5.5M;
Console.WriteLine(TimeSpan.FromHours((double)t).ToString());

That'll give you "05:30:00" which is pretty close. You could then format that to your desired result:

var ts = TimeSpan.FromHours((double)t);
Console.WriteLine("{0} hrs {1} minutes", ts.Hours, ts.Minutes);

Note that there's a potential for loss of accuracy in the conversion from decimal to double, but I don't think it would be noticeable on the minute scale. Someone with a Skeet-like understanding of the type system might be able to chime in here.

太阳哥哥 2024-11-11 14:59:49

如果您的时间可能超过 24 小时,则必须考虑 TimeSpan 类具有 Days 属性,并且 Hours 属性将循环。这样可以防止超过 24 小时的任何错误:

 var tsExample = TimeSpan.FromHours(timeInDecimal);
 var result = $"{tsExample.Hours + (tsExample.Days * 24):00}:{tsExample.Minutes:00}";

If you have time that can be more than 24 hours, you have to consider that the TimeSpan class has a Days property, and the Hours property will cycle. Something like this will prevent any error from time with more than 24 hours:

 var tsExample = TimeSpan.FromHours(timeInDecimal);
 var result = $"{tsExample.Hours + (tsExample.Days * 24):00}:{tsExample.Minutes:00}";
樱娆 2024-11-11 14:59:49

我根据 Matt 的评论为此编写了一些小辅助方法。对于某人来说可能有用,可以让您自己编写它。

/// <summary>Converts a decimal e.g. 1.5 to 1 hour 30 minutes</summary>
/// <param name="duration">The time to convert as a double</param>
/// <returns>
///     Returns a string in format:
///     x hours x minutes
///     x hours (if there's no minutes)
///     x minutes (if there's no hours)
///     Will also pluralise the words if required e.g. 1 hour or 3 hours
/// </returns>
public String convertDecimalToHoursMinutes(double time)
{
    TimeSpan timespan = TimeSpan.FromHours(time);
    int hours = timespan.Hours;
    int mins = timespan.Minutes;

    // Convert to hours and minutes
    String hourString = (hours > 0) ? string.Format("{0} " + pluraliseTime(hours, "hour"), hours) : "";
    String minString = (mins > 0) ? string.Format("{0} " + pluraliseTime(mins, "minute"), mins) : "";

    // Add a space between the hours and minutes if necessary
    return (hours > 0 && mins > 0) ? hourString + " " + minString : hourString + minString;
}

/// <summary>Pluralise hour or minutes based on the amount of time</summary>
/// <param name="num">The number of hours or minutes</param>
/// <param name="word">The word to pluralise e.g. "hour" or "minute"</param>
/// <returns> Returns correct English pluralisation e.g. 3 hours, 1 minute, 0 minutes</returns>
public String pluraliseTime(int num, String word)
{
    return (num == 0 || num > 1) ? word + "s" : word;
}

I wrote a few small helper methods for this based on Matt's comment. Might be useful for someone to save you writing it yourself.

/// <summary>Converts a decimal e.g. 1.5 to 1 hour 30 minutes</summary>
/// <param name="duration">The time to convert as a double</param>
/// <returns>
///     Returns a string in format:
///     x hours x minutes
///     x hours (if there's no minutes)
///     x minutes (if there's no hours)
///     Will also pluralise the words if required e.g. 1 hour or 3 hours
/// </returns>
public String convertDecimalToHoursMinutes(double time)
{
    TimeSpan timespan = TimeSpan.FromHours(time);
    int hours = timespan.Hours;
    int mins = timespan.Minutes;

    // Convert to hours and minutes
    String hourString = (hours > 0) ? string.Format("{0} " + pluraliseTime(hours, "hour"), hours) : "";
    String minString = (mins > 0) ? string.Format("{0} " + pluraliseTime(mins, "minute"), mins) : "";

    // Add a space between the hours and minutes if necessary
    return (hours > 0 && mins > 0) ? hourString + " " + minString : hourString + minString;
}

/// <summary>Pluralise hour or minutes based on the amount of time</summary>
/// <param name="num">The number of hours or minutes</param>
/// <param name="word">The word to pluralise e.g. "hour" or "minute"</param>
/// <returns> Returns correct English pluralisation e.g. 3 hours, 1 minute, 0 minutes</returns>
public String pluraliseTime(int num, String word)
{
    return (num == 0 || num > 1) ? word + "s" : word;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文