Archive for Maggio 28th, 2008

Human readable file sizes (25 MB, 2 GB) from file size inbytes

CodeKeep C# Feed Maggio 28th, 2008

Description: Human readable file sizes (25 MB, 2 GB) from file size inbytes

Link: http://www.codekeep.net/snippets/b9dcda3e-5454-4ee7-b816-bf111c56c193.aspx

string GetReadableFileSize(long fileSize) {
      long s = size;
      string[] format = new string[] { "{0} bytes", "{0} KB", "{0} MB", "{0} GB", "{0} TB", "{0} PB", "{0} EB", "{0} ZB", "{0} YB" };
      int i = 0;
      while (i < format.Length - 1 && s >= 1024) {
        s = (long)((long)100 * s / (long)1024) / (long)100.0;
        i++;

      }
      return string.Format(format[i], s.ToString("###,###,###.##"));
    }