2014-10-27 05:51:50 +00:00
|
|
|
|
using System;
|
2014-12-21 19:08:26 +00:00
|
|
|
|
using System.Globalization;
|
2014-10-27 05:51:50 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
|
public static string SizeSuffix(this long bytes)
|
2014-10-27 05:51:50 +00:00
|
|
|
|
{
|
2015-08-29 21:49:06 +00:00
|
|
|
|
const int bytesInKb = 1024;
|
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
|
|
|
|
|
2014-12-21 19:08:26 +00:00
|
|
|
|
return string.Format(CultureInfo.InvariantCulture, "{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
|
2014-10-27 05:51:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|