using System; using System.Collections.Generic; using System.Linq; using zero.Core.Entities; namespace zero.Core.Extensions { public static class NumberExtensions { static Dictionary FileSizeUnits = new() { { FileSizeNotation.SI, new string[] { "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" } }, { FileSizeNotation.IEC, new string[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" } }, { FileSizeNotation.JEDEC, new string[] { "B", "KB", "MB", "GB" } } }; public static int Limit(this int input, int min, int max) { if (input < min) { return min; } if (input > max) { return max; } return input; } public static string GetFileSize(this long sizeInBytes, FileSizeNotation notation = FileSizeNotation.JEDEC) { if (!FileSizeUnits.ContainsKey(notation)) { throw new NotImplementedException($"The notation {notation} has no implementation for generating human-readable file sizes"); } int power = notation == FileSizeNotation.SI ? 1000 : 1024; string[] units = FileSizeUnits[notation]; int order = 0; while (sizeInBytes >= power && order + 1 < units.Length) { order++; sizeInBytes = sizeInBytes / power; } return String.Format("{0:0.##} {1}", sizeInBytes, units[order]); } } }