Advertisement
giammin

Convert byte to KB MB GB

Oct 10th, 2013
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | None | 0 0
  1. public static string ToKMGTByte(ulong bytes)
  2. {
  3.     var suffix = new[] { "B", "KB", "MB", "GB", "TB","PB" };
  4.  
  5.     int idx = 0;
  6.     ulong rtn=bytes;
  7.     while (idx<suffix.Length-1 && (bytes >>= 10) > 0)
  8.     {
  9.         rtn = bytes;
  10.         idx++;
  11.     }
  12.     return string.Concat(rtn, suffix[idx]);
  13. }
  14.  
  15.         public static string ConvertToReadbleFileSize(long size)
  16.         {
  17.             return size > 1000000 ?
  18.                 (Math.Round((double)(size / (1024 * 1024)), 2)).ToString("0.00 GB") :
  19.                 (Math.Round((double)(size / 1024), 2)).ToString("0.00 MB");
  20.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement