summaryrefslogtreecommitdiff
path: root/Util.cs
blob: ae33e7ea934f33ad7eb8a1a2f00856187ac40106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using HyperBooru.ApiModels;

namespace HyperBooru.Client.Util;

public static class Extensions {
    public static readonly string[] MagnitudeOrders = new[] {
        "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"
    };

    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";
    }
}