Esempio n. 1
0
        public static string FormatSize(this SizeUnit sizeUnit, ulong size)
        {
            var prefix = sizeUnit.DeterminePrefix(size);

            var denominator = 1.0;

            for (var i = 0; i < prefix; i++)
            {
                denominator *= (ulong)sizeUnit;
            }

            var formattedSize = size / denominator;

            if (prefix == 0)
            {
                sizeUnit = SizeUnit.Bytes;
            }

            if (formattedSize >= 1000 && sizeUnit == SizeUnit.Binary)
            {
                prefix++;
                formattedSize /= (double)sizeUnit;
            }

            var prefixSign = sizeUnit switch
            {
                SizeUnit.Bytes => "B",
                SizeUnit.Binary => $"{prefixes[prefix]}iB",
                SizeUnit.Decimal => $"{prefixes[prefix]}B",
                _ => throw new ArgumentException()
            };

            return($"{formattedSize:#,0.##} {prefixSign}");
        }