Last active
December 11, 2015 04:38
-
-
Save sebnilsson/4546157 to your computer and use it in GitHub Desktop.
Get human-readable file-sizes
This file contains 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
public class FileSizeHelper | |
{ | |
private static readonly string[] Units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; | |
private static string GetReadableFileSize(long size) // Size in bytes | |
{ | |
int unitIndex = 0; | |
while (size >= 1024) | |
{ | |
size /= 1024; | |
++unitIndex; | |
} | |
string unit = Units[unitIndex]; | |
return string.Format("{0:0.#} {1}", size, unit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment