Skip to content

Instantly share code, notes, and snippets.

@lawliet89
Created June 18, 2015 11:33
Show Gist options
  • Save lawliet89/5bf45a4af8c58c61ea41 to your computer and use it in GitHub Desktop.
Save lawliet89/5bf45a4af8c58c61ea41 to your computer and use it in GitHub Desktop.
BytesToString
public static string BytesToString(long byteSize)
{
const string format = "{0} {1}";
const int orderBase = 1024;
var units = new[] {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
if (byteSize == 0)
return "0 B";
var order = Convert.ToInt32(Math.Floor(Math.Log(byteSize, orderBase)));
var rescaledSize = Math.Round(byteSize/Math.Pow(orderBase, order), 1);
return string.Format(format, rescaledSize, units[order]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment