Created
December 18, 2013 10:41
-
-
Save dampee/8020337 to your computer and use it in GitHub Desktop.
format size as bytes, kb, mb, gb
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 string FormatBytes(long bytes, int scale = 1024) | |
{ | |
string[] orders = new string[] { "GB", "MB", "KB", "Bytes" }; | |
long max = (long)Math.Pow(scale, orders.Length - 1); | |
foreach (string order in orders) | |
{ | |
if (bytes > max) | |
{ | |
return String.Format("{0:##.##} {1}", Decimal.Divide(bytes, max), order); | |
} | |
max /= scale; | |
} | |
return "0 Bytes"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment