Skip to content

Instantly share code, notes, and snippets.

@maftieu
Last active August 29, 2015 14:11
Show Gist options
  • Save maftieu/9a3d1532aa968e68f6a2 to your computer and use it in GitHub Desktop.
Save maftieu/9a3d1532aa968e68f6a2 to your computer and use it in GitHub Desktop.
C# - FormatFileSize
static string FormatFileSize(long value, string format = null)
{
var multipliers = new string[] { "o", "Kio", "Mio" };
var midx = 0;
while ((Math.Abs(value) >= 1024) && (midx < multipliers.Length))
{
value /= 1000;
++midx;
}
var unit = multipliers[midx];
return string.Format("{0} {1}", value.ToString(format ?? ""), unit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment