Mega Code Archive

 
Categories / C# / Data Types
 

Formats the specified size as a string

using System; using System.Collections.Generic; using System.Text; public enum ByteSizeFormat {     Bytes = 0,     Kilobytes = 1,     Megabytes = 2,     Gigabytes = 3 } public static class FormatUtility {     public static string FormatByteSize(int fileSize, ByteSizeFormat format)     {         float value = 0;         int divisor = 0;         string unit = string.Empty;         switch (format)         {             case ByteSizeFormat.Bytes:                 divisor = 1;                 unit = " byte(s)";                 break;             case ByteSizeFormat.Kilobytes:                 divisor = 1024;                 unit = " KB";                 break;             case ByteSizeFormat.Megabytes:                 divisor = 1024 * 1024;                 unit = " MB";                 break;             case ByteSizeFormat.Gigabytes:                 divisor = 1024 * 1024 * 1024;                 unit = " GB";                 break;         }         value = (fileSize / divisor);         return value.ToString("###,###,###,###,###,###,##0.0") + unit;     } }