File size to String
29-12-2015 10:59:48
C# / IO
0 Bookmark(s)
226 View(s)
public static class FileSizeHelper
{
public static string ToFileSize(this long size)
{
var units = new[] { "bytes", "KB", "MB", "GB", "TB" };
var index = 0;
double dSize = size;
while (dSize > 1024)
{
dSize /= 1024;
index++;
}
if (index < 2)
{
return string.Format("{0:0} {1}", dSize, units[index]);
}
return string.Format("{0:0.00} {1}", dSize, units[index]);
}
}