2014-10-27 05:51:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class Int64Extensions
|
|
|
|
|
{
|
2014-12-15 21:08:22 +00:00
|
|
|
|
private static readonly string[] SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
|
2014-10-27 05:51:50 +00:00
|
|
|
|
|
2014-12-15 21:08:22 +00:00
|
|
|
|
public static string SizeSuffix(this Int64 bytes)
|
2014-10-27 05:51:50 +00:00
|
|
|
|
{
|
2014-12-15 20:16:01 +00:00
|
|
|
|
const int bytesInKb = 1000;
|
2014-10-27 05:51:50 +00:00
|
|
|
|
|
2014-12-15 21:08:22 +00:00
|
|
|
|
if (bytes < 0) return "-" + SizeSuffix(-bytes);
|
|
|
|
|
if (bytes == 0) return "0 B";
|
|
|
|
|
|
|
|
|
|
var mag = (int)Math.Log(bytes, bytesInKb);
|
|
|
|
|
var adjustedSize = bytes / (decimal)Math.Pow(bytesInKb, mag);
|
2014-10-27 05:51:50 +00:00
|
|
|
|
|
|
|
|
|
return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|