/ Framework / Utils / Formatter.cs
Formatter.cs
 1  using System.Globalization;
 2  
 3  namespace Utils
 4  {
 5      public static class Formatter
 6      {
 7          private static readonly string[] sizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
 8  
 9          public static string FormatByteSize(long bytes)
10          {
11              if (bytes == 0) return "0" + sizeSuffixes[0];
12  
13              var sizeOrder = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
14              var digit = Math.Round(bytes / Math.Pow(1024, sizeOrder), 1);
15              return digit.ToString(CultureInfo.InvariantCulture) + sizeSuffixes[sizeOrder];
16          }
17      }
18  }