From d4f0eae4dc54f356f296ff26aba006e69d21ec0b Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 29 Aug 2023 13:53:23 +1000 Subject: Added file size to ViewMedia page --- Util.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'Util.cs') 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"; + } } -- cgit v1.3