Skip to content

Instantly share code, notes, and snippets.

@jrt324
Created October 1, 2019 05:15
Show Gist options
  • Save jrt324/4f7752fa4bfc8f99a89ee98340831b3e to your computer and use it in GitHub Desktop.
Save jrt324/4f7752fa4bfc8f99a89ee98340831b3e to your computer and use it in GitHub Desktop.
File size friendly display
private string ToFileSizeString(long size)
{
string result;
if (size < 1000L)
{
result = string.Format("{0} bytes", size);
}
else if (size < 1000000L)
{
result = string.Format("{0:F1} KB", (double)size / 1000.0);
}
else if (size < 1000000000L)
{
result = string.Format("{0:F1} MB", (double)size / 1000000.0);
}
else if (size < 1000000000000L)
{
result = string.Format("{0:F1} GB", (double)size / 1000000000.0);
}
else if (size < 1000000000000000L)
{
result = string.Format("{0:F1} TB", (double)size / 1000000000000.0);
}
else
{
result = size.ToString(CultureInfo.InvariantCulture);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment