Created
June 18, 2015 11:33
-
-
Save lawliet89/5bf45a4af8c58c61ea41 to your computer and use it in GitHub Desktop.
BytesToString
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 BytesToString(long byteSize) | |
{ | |
const string format = "{0} {1}"; | |
const int orderBase = 1024; | |
var units = new[] {"B", "KB", "MB", "GB", "TB", "PB", "EB"}; | |
if (byteSize == 0) | |
return "0 B"; | |
var order = Convert.ToInt32(Math.Floor(Math.Log(byteSize, orderBase))); | |
var rescaledSize = Math.Round(byteSize/Math.Pow(orderBase, order), 1); | |
return string.Format(format, rescaledSize, units[order]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment