andrew4582

ByteFormatter -Utility for formatting numbers into Bytes, KB,MB,GB, and TB

Jul 28th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. /// <summary>
  2.     /// Utility for formatting numbers into Bytes, KB,MB,GB, and TB
  3.     /// </summary>
  4.     public static class ByteFormatter
  5.     {
  6.         public const bool WITH_DIGITS_DEFAULT = true;
  7.         public const long KB = 1024;
  8.         public const long MB = KB * 1024;
  9.         public const long GB = MB * 1024;
  10.         public const long TB = GB * 1024;
  11.  
  12.         public const string BFormatPattern = "{0:N0} bytes";
  13.         public const string KBFormatPattern = "{0:N0} KB";
  14.         public const string MBFormatPattern = "{0:N0} MB";
  15.         public const string GBFormatPattern = "{0:N0} GB";
  16.         public const string TBFormatPattern = "{0:N0} TB";
  17.  
  18.         public const string KBFormatPattern_wDigits = "{0:N2} KB";
  19.         public const string MBFormatPattern_wDigits = "{0:N2} MB";
  20.         public const string GBFormatPattern_wDigits = "{0:N2} GB";
  21.         public const string TBFormatPattern_wDigits = "{0:N2} TB";
  22.  
  23.         public static string ToFormat(double size)
  24.         {
  25.             return ToFormat(size,WITH_DIGITS_DEFAULT);
  26.         }
  27.  
  28.         public static string ToFormat(double size,bool withDigits)
  29.         {
  30.  
  31.             string kbfmt = KBFormatPattern;
  32.             string mbfmt = MBFormatPattern;
  33.             string gbfmt = GBFormatPattern;
  34.             string tbfmt = TBFormatPattern;
  35.  
  36.             if(withDigits)
  37.             {
  38.                 kbfmt = KBFormatPattern_wDigits;
  39.                 mbfmt = MBFormatPattern_wDigits;
  40.                 gbfmt = GBFormatPattern_wDigits;
  41.                 tbfmt = TBFormatPattern_wDigits;
  42.             }
  43.             if(size < KB)
  44.             {
  45.                 return String.Format(BFormatPattern,size);
  46.             }
  47.             else if(size >= KB && size < MB)
  48.             {
  49.                 return String.Format(kbfmt,size / 1024.0f);
  50.             }
  51.             else if(size >= MB && size < GB)
  52.             {
  53.                 return String.Format(mbfmt,(size / 1024.0f) / 1024.0f);
  54.             }
  55.             else if(size >= GB && size < TB)
  56.             {
  57.                 return String.Format(gbfmt,((size / 1024.0f) / 1024.0f) / 1024.0f);
  58.             }
  59.             else if(size >= TB)
  60.             {
  61.                 return String.Format(tbfmt,(((size / 1024.0f) / 1024.0f) / 1024.0f) / 1024f);
  62.             }
  63.             else
  64.                 return String.Format(tbfmt,(((size / 1024.0f) / 1024.0f) / 1024.0f) / 1024f);
  65.  
  66.         }
  67.  
  68.         public static string ToString(long size,bool withDigits = WITH_DIGITS_DEFAULT)
  69.         {
  70.             return ToFormat(size,withDigits);
  71.         }
  72.  
  73.         public static string FormatBytes(this short size,bool withDigits = WITH_DIGITS_DEFAULT)
  74.         {
  75.             return ToFormat(size,withDigits);
  76.         }
  77.  
  78.         public static string FormatBytes(this int size,bool withDigits = WITH_DIGITS_DEFAULT)
  79.         {
  80.             return ToFormat(size,withDigits);
  81.         }
  82.  
  83.         public static string FormatBytes(this long size,bool withDigits = WITH_DIGITS_DEFAULT)
  84.         {
  85.             return ToFormat(size,withDigits);
  86.         }
  87.  
  88.         public static string FormatBytes(this decimal size,bool withDigits = WITH_DIGITS_DEFAULT)
  89.         {
  90.             return ToFormat((double)size,withDigits);
  91.         }
  92.         public static string FormatBytes(this double size,bool withDigits = WITH_DIGITS_DEFAULT)
  93.         {
  94.             return ToFormat(size,withDigits);
  95.         }
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment