Radarr/src/NzbDrone.Common/Extensions/Int64Extensions.cs

31 lines
804 B
C#
Raw Normal View History

using System;
2014-12-21 19:08:26 +00:00
using System.Globalization;
namespace NzbDrone.Common.Extensions
{
public static class Int64Extensions
{
private static readonly string[] SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
public static string SizeSuffix(this long bytes)
{
const int bytesInKb = 1024;
2019-12-22 22:08:53 +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-12-21 19:08:26 +00:00
return string.Format(CultureInfo.InvariantCulture, "{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
}
}
}