37 lines
934 B
C#
37 lines
934 B
C#
|
|
namespace WorkTracker.Formatting;
|
||
|
|
|
||
|
|
public static class DurationFormatter
|
||
|
|
{
|
||
|
|
public static string FormatHours(decimal value, bool blankWhenZero = false)
|
||
|
|
{
|
||
|
|
var totalMinutes = (int)Math.Round(value * 60m, MidpointRounding.AwayFromZero);
|
||
|
|
if (totalMinutes == 0)
|
||
|
|
{
|
||
|
|
return blankWhenZero ? string.Empty : "0";
|
||
|
|
}
|
||
|
|
|
||
|
|
var sign = totalMinutes < 0 ? "-" : string.Empty;
|
||
|
|
totalMinutes = Math.Abs(totalMinutes);
|
||
|
|
|
||
|
|
var hours = totalMinutes / 60;
|
||
|
|
var minutes = totalMinutes % 60;
|
||
|
|
return minutes == 0
|
||
|
|
? $"{sign}{hours}"
|
||
|
|
: $"{sign}{hours}:{minutes:00}";
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string FormatSignedHours(decimal value)
|
||
|
|
{
|
||
|
|
if (value > 0m)
|
||
|
|
{
|
||
|
|
return $"+{FormatHours(value)}";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (value < 0m)
|
||
|
|
{
|
||
|
|
return $"-{FormatHours(Math.Abs(value))}";
|
||
|
|
}
|
||
|
|
|
||
|
|
return "0";
|
||
|
|
}
|
||
|
|
}
|