From 6d31b14d96922ec12efeead0e10e1297b2e3e9c2 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 15 Dec 2014 12:16:01 -0800 Subject: [PATCH] Fixed: Humanized size show same values as size settings --- .../ExtensionTests/Int64ExtensionFixture.cs | 19 +++++++++++++++++++ .../NzbDrone.Common.Test.csproj | 1 + .../Extensions/Int64Extensions.cs | 10 ++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/NzbDrone.Common.Test/ExtensionTests/Int64ExtensionFixture.cs diff --git a/src/NzbDrone.Common.Test/ExtensionTests/Int64ExtensionFixture.cs b/src/NzbDrone.Common.Test/ExtensionTests/Int64ExtensionFixture.cs new file mode 100644 index 000000000..5984e45ec --- /dev/null +++ b/src/NzbDrone.Common.Test/ExtensionTests/Int64ExtensionFixture.cs @@ -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); + } + } +} diff --git a/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj b/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj index a4199d64b..f0c402849 100644 --- a/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj +++ b/src/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj @@ -75,6 +75,7 @@ + diff --git a/src/NzbDrone.Common/Extensions/Int64Extensions.cs b/src/NzbDrone.Common/Extensions/Int64Extensions.cs index d389e5969..9021cfde3 100644 --- a/src/NzbDrone.Common/Extensions/Int64Extensions.cs +++ b/src/NzbDrone.Common/Extensions/Int64Extensions.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]); }