1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-23 22:25:14 -04:00

More test cases for human readable bytes

This commit is contained in:
Mark McDowall
2014-12-15 13:08:22 -08:00
parent e715275657
commit c10f02e0b1
2 changed files with 11 additions and 7 deletions
@@ -4,17 +4,17 @@ namespace NzbDrone.Common.Extensions
{
public static class Int64Extensions
{
private static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private static readonly string[] SizeSuffixes = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
public static string SizeSuffix(this Int64 value)
public static string SizeSuffix(this Int64 bytes)
{
const int bytesInKb = 1000;
if (value < 0) return "-" + SizeSuffix(-value);
if (value == 0) return "0 bytes";
var mag = (int)Math.Log(value, bytesInKb);
var adjustedSize = value / (decimal)Math.Pow(bytesInKb, mag);
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);
return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
}