summaryrefslogtreecommitdiff
path: root/Util.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-08-29 13:53:23 +1000
committerJake Mannens <jake@asger.xyz>2025-08-20 00:48:44 +1000
commitd4f0eae4dc54f356f296ff26aba006e69d21ec0b (patch)
tree715e26a52e20dcc12e7502fe9cf33e36ea988dd8 /Util.cs
parent4aa0c3d2c063040f4ad0a85c8ceb0aace48147f7 (diff)
Added file size to ViewMedia page
Diffstat (limited to 'Util.cs')
-rw-r--r--Util.cs13
1 files changed, 13 insertions, 0 deletions
diff --git a/Util.cs b/Util.cs
index 9cbccdf..31a2e84 100644
--- a/Util.cs
+++ b/Util.cs
@@ -1,8 +1,21 @@
namespace HyperBooru.Util;
public static class Extensions {
+ public static readonly string[] MagnitudeOrders = new[] {
+ "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"
+ };
+
public static DateTime? TryParseDateTimeUtc(this string s) {
bool success = DateTime.TryParse(s, out var dateTime);
return success ? DateTime.SpecifyKind(dateTime, DateTimeKind.Utc) : null;
}
+
+ public static string ToBytesSI(this long x) {
+ var exp = (int) Math.Log10(x);
+ var suffix = MagnitudeOrders.ElementAtOrDefault(exp / 3 - 1);
+ if(suffix is null)
+ return x.ToString();
+ double n = x / Math.Pow(10, exp / 3 * 3);
+ return $"{Math.Round(n, 2 - (exp % 3))} {suffix}B";
+ }
}