diff --git a/ProxyForge/ViewModels/ByteText.cs b/ProxyForge/ViewModels/ByteText.cs new file mode 100644 index 0000000..5d1a58d --- /dev/null +++ b/ProxyForge/ViewModels/ByteText.cs @@ -0,0 +1,24 @@ +using System.Globalization; + +namespace ProxyForge.ViewModels; + +internal static class ByteText +{ + static readonly string[] Units = ["B", "KB", "MB", "GB", "TB"]; + + public static string Format(long bytes) + { + if (bytes <= 0) + return "0 B"; + + double value = bytes; + var order = 0; + while (value >= 1024d && order < Units.Length - 1) + { + value /= 1024d; + order++; + } + + return string.Create(CultureInfo.InvariantCulture, $"{value:0.##} {Units[order]}"); + } +}