Created
December 19, 2021 05:11
-
-
Save benrr101/0f7e6940e842ba16efa02436f42ccf9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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