[backend/core] Refactor NumberExtensions

This commit is contained in:
Laura Hausmann 2024-06-21 16:02:40 +02:00
parent 6adfa87966
commit 9319dc22c9
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -20,39 +20,20 @@ public static class NumberExtensions
return result.ToString();
}
public static string ToBase36(this int input)
{
if (input == 0) return "0";
var result = new StringBuilder();
while (input >= 0)
{
result.Insert(0, Base36Charset[input % 36]);
input /= 36;
}
return result.ToString();
}
public static string ToBase36(this int input) => ToBase36((long)input);
public static string ToDurationDisplayString(this long input)
{
var ts = TimeSpan.FromMilliseconds(input);
return input switch
{
< 1000 => $"{input} ms",
< 1000 * 60 => $"{Math.Round(input / 1000d / 60d, 2)} s",
< 1000 * 60 * 60 => $"{Math.Round(input / 60000d / 60d, 2)} m",
_ => $"{Math.Round(input / 60000d / 60d / 60d, 2)} h"
};
}
public static string ToDurationDisplayString(this int input)
{
return input switch
{
< 1000 => $"{input} ms",
< 1000 * 60 => $"{Math.Round(input / 1000d / 60d, 2)} s",
< 1000 * 60 * 60 => $"{Math.Round(input / 60000d / 60d, 2)} m",
_ => $"{Math.Round(input / 60000d / 60d / 60d, 2)} h"
< 1000 * 60 => $"{Math.Round(input / 1000d, 2)} s",
< 1000 * 60 * 60 => $"{ts.Minutes}m {ts.Seconds}s",
_ => $"{Math.Truncate(ts.TotalHours)}h {ts.Minutes}m"
};
}
public static string ToDurationDisplayString(this int input) => ToDurationDisplayString((long)input);
}