Skip to content

Instantly share code, notes, and snippets.

@benrr101
Created December 19, 2021 05:11
Show Gist options
  • Save benrr101/0f7e6940e842ba16efa02436f42ccf9a to your computer and use it in GitHub Desktop.
Save benrr101/0f7e6940e842ba16efa02436f42ccf9a to your computer and use it in GitHub Desktop.
private static string GetFriendlyByteString(long bytes)
{
string[] units = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
double runningTotal = bytes;
foreach (var unit in units)
{
double dividedTotal = runningTotal / 1024;
if (dividedTotal < 1)
{
return $"{runningTotal:F2} {unit}";
}
runningTotal = dividedTotal;
}
// Heuristically unreachable.
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment