Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created July 31, 2019 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonsmits/d4f0f97b188ef615c0edf57eb714cccc to your computer and use it in GitHub Desktop.
Save ramonsmits/d4f0f97b188ef615c0edf57eb714cccc to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-in-bytes-abbreviation-using-net
public static class SizeFormatter
{
static String StrFormatByteSize (long length, IFormatProvider provider)
{
const int kb = 1024;
string[] suffix = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
if (length == 0) return "0" + suffix[0];
var bytes = Math.Abs(length);
var index = (int)Math.Floor(Math.Log(bytes, kb));
var num = bytes / Math.Pow(kb, index);
return (Math.Sign(length) * num).ToString("N", provider) + suffix[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment