Created
January 9, 2015 15:57
-
-
Save feanz/70ed5f9919a7dc8c51f4 to your computer and use it in GitHub Desktop.
To File Size Extensions method
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
public static class MiscExtensions | |
{ | |
public static string ToFileSize(this long size) | |
{ | |
if (size < 1024) { return (size).ToString("F0") + " bytes"; } | |
if (size < Math.Pow(1024, 2)) { return (size/1024).ToString("F0") + "KB"; } | |
if (size < Math.Pow(1024, 3)) { return (size/Math.Pow(1024, 2)).ToString("F0") + "MB"; } | |
if (size < Math.Pow(1024, 4)) { return (size/Math.Pow(1024, 3)).ToString("F0") + "GB"; } | |
if (size < Math.Pow(1024, 5)) { return (size/Math.Pow(1024, 4)).ToString("F0") + "TB"; } | |
if (size < Math.Pow(1024, 6)) { return (size/Math.Pow(1024, 5)).ToString("F0") + "PB"; } | |
return (size/Math.Pow(1024, 6)).ToString("F0") + "EB"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment