diff options
| author | Jake Mannens <jake@asger.xyz> | 2023-08-29 13:53:23 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2023-08-29 13:53:23 +1000 |
| commit | 95a2d3467dcc7b28dd6eedc1405e097c947c7bc4 (patch) | |
| tree | e47a5381fbcfd8de54e0b44e5f7ef4116daed2da /Util.cs | |
| parent | f63484b7d5090cacc6c14918e936824154c85093 (diff) | |
Added file size to ViewMedia page
Diffstat (limited to 'Util.cs')
| -rw-r--r-- | Util.cs | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -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"; + } } |
