Skip to content

Instantly share code, notes, and snippets.

@feanz
Created January 9, 2015 15:57
Show Gist options
  • Save feanz/70ed5f9919a7dc8c51f4 to your computer and use it in GitHub Desktop.
Save feanz/70ed5f9919a7dc8c51f4 to your computer and use it in GitHub Desktop.
To File Size Extensions method
public static class MiscExtensions
{
public static string ToFileSize(this long size)
{
if (size < 1024) { return (size).ToString("F0") + " bytes"; }
if (size < Math.Pow(1024, 2)) { return (size/1024).ToString("F0") + "KB"; }
if (size < Math.Pow(1024, 3)) { return (size/Math.Pow(1024, 2)).ToString("F0") + "MB"; }
if (size < Math.Pow(1024, 4)) { return (size/Math.Pow(1024, 3)).ToString("F0") + "GB"; }
if (size < Math.Pow(1024, 5)) { return (size/Math.Pow(1024, 4)).ToString("F0") + "TB"; }
if (size < Math.Pow(1024, 6)) { return (size/Math.Pow(1024, 5)).ToString("F0") + "PB"; }
return (size/Math.Pow(1024, 6)).ToString("F0") + "EB";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment