andrew4582

FormatByte & FormatBytesPerSecond

Nov 12th, 2010
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1.         private string FormatByte(long bytes, int rounding)
  2.         {
  3.             if (bytes >= System.Math.Pow(2, 80))
  4.                 return System.Math.Round(bytes / System.Math.Pow(2, 70), rounding).ToString() + " YB"; //yettabyte
  5.             if (bytes >= System.Math.Pow(2, 70))
  6.                 return System.Math.Round(bytes / System.Math.Pow(2, 70), rounding).ToString() + " ZB"; //zettabyte
  7.             if (bytes >= System.Math.Pow(2, 60))
  8.                 return System.Math.Round(bytes / System.Math.Pow(2, 60), rounding).ToString() + " EB"; //exabyte
  9.             if (bytes >= System.Math.Pow(2, 50))
  10.                 return System.Math.Round(bytes / System.Math.Pow(2, 50), rounding).ToString() + " PB"; //petabyte
  11.             if (bytes >= System.Math.Pow(2, 40))
  12.                 return System.Math.Round(bytes / System.Math.Pow(2, 40), rounding).ToString() + " TB"; //terabyte
  13.             if (bytes >= System.Math.Pow(2, 30))
  14.                 return System.Math.Round(bytes / System.Math.Pow(2, 30), rounding).ToString() + " GB"; //gigabyte
  15.             if (bytes >= System.Math.Pow(2, 20))
  16.                 return System.Math.Round(bytes / System.Math.Pow(2, 20), rounding).ToString() + " MB"; //megabyte
  17.             if (bytes >= System.Math.Pow(2, 10))
  18.                 return System.Math.Round(bytes / System.Math.Pow(2, 10), rounding).ToString() + " KB"; //kilobyte
  19.  
  20.             return bytes.ToString() + " Bytes"; //byte
  21.         }
  22.  
  23.         private string FormatBytesPerSecond(long bytes, double secounds, int rounding)
  24.         {
  25.             double bytesPerSecounds = bytes / secounds;
  26.  
  27.             if (bytesPerSecounds >= System.Math.Pow(2, 80))
  28.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 70), rounding).ToString() + " YB/s"; //yettabyte
  29.             if (bytesPerSecounds >= System.Math.Pow(2, 70))
  30.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 70), rounding).ToString() + " ZB/s"; //zettabyte
  31.             if (bytesPerSecounds >= System.Math.Pow(2, 60))
  32.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 60), rounding).ToString() + " EB/s"; //exabyte
  33.             if (bytesPerSecounds >= System.Math.Pow(2, 50))
  34.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 50), rounding).ToString() + " PB/s"; //petabyte
  35.             if (bytesPerSecounds >= System.Math.Pow(2, 40))
  36.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 40), rounding).ToString() + " TB/s"; //terabyte
  37.             if (bytesPerSecounds >= System.Math.Pow(2, 30))
  38.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 30), rounding).ToString() + " GB/s"; //gigabyte
  39.             if (bytesPerSecounds >= System.Math.Pow(2, 20))
  40.                 return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 20), rounding).ToString() + " MB/s"; //megabyte
  41.  
  42.             return System.Math.Round(bytesPerSecounds / System.Math.Pow(2, 10), rounding).ToString() + " KB/s"; //kilobyte
  43.         }
Advertisement
Add Comment
Please, Sign In to add comment