Fixed: Humanized size show same values as size settings

pull/2/head
Mark McDowall 10 years ago
parent 83e3d7145f
commit 6d31b14d96

@ -0,0 +1,19 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Common.Test.ExtensionTests
{
[TestFixture]
public class Int64ExtensionFixture
{
[TestCase(1000, "1.0 KB")]
[TestCase(1000000, "1.0 MB")]
[TestCase(377487360, "377.5 MB")]
[TestCase(1255864686, "1.3 GB")]
public void should_calculate_string_correctly(long bytes, string expected)
{
bytes.SizeSuffix().Should().Be(expected);
}
}
}

@ -75,6 +75,7 @@
<Compile Include="EnvironmentProviderTest.cs" />
<Compile Include="EnvironmentTests\EnvironmentProviderTest.cs" />
<Compile Include="EnvironmentTests\StartupArgumentsFixture.cs" />
<Compile Include="ExtensionTests\Int64ExtensionFixture.cs" />
<Compile Include="Http\HttpClientFixture.cs" />
<Compile Include="Http\HttpRequestBuilderFixture.cs" />
<Compile Include="Http\HttpRequestFixture.cs" />

@ -8,11 +8,13 @@ namespace NzbDrone.Common.Extensions
public static string SizeSuffix(this Int64 value)
{
if (value < 0) { return "-" + SizeSuffix(-value); }
if (value == 0) { return "0.0 bytes"; }
const int bytesInKb = 1000;
var mag = (int)Math.Log(value, 1024);
decimal adjustedSize = (decimal)value / (1L << (mag * 10));
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);
return string.Format("{0:n1} {1}", adjustedSize, SizeSuffixes[mag]);
}

Loading…
Cancel
Save