Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class DisplayStringHelperTests
{
private const long Gibibyte = 1073741824;
private const long Mebibyte = 1048576;

[Test]
public void GenerateNumberWithLimitDisplayString_returns_expected_string_with_limit()
Expand Down Expand Up @@ -46,7 +47,7 @@ public void FormatBytesWithLimit_returns_expected_string_for_kilobytes()
var result = DisplayStringHelper.FormatBytesWithLimit(12, 1200);

// Then
result.Should().Be("12B / 1.2KiB");
result.Should().Be("12B / 1KB");
}

[Test]
Expand All @@ -56,7 +57,27 @@ public void FormatBytesWithLimit_returns_expected_string_for_gibibytes()
var result = DisplayStringHelper.FormatBytesWithLimit(12, Gibibyte);

// Then
result.Should().Be("12B / 1GiB");
result.Should().Be("12B / 1GB");
}

[Test]
public void FormatBytesWithLimit_returns_expected_string_for_gibibytes_with_decimal()
{
// When
var result = DisplayStringHelper.FormatBytesWithLimit(12, Gibibyte + 500000000);

// Then
result.Should().Be("12B / 1.5GB");
}

[Test]
public void FormatBytesWithLimit_returns_expected_string_for_mebibytes_with_decimal()
{
// When
var result = DisplayStringHelper.FormatBytesWithLimit(12, Mebibyte + 800000);

// Then
result.Should().Be("12B / 2MB");
}

[Test]
Expand All @@ -69,7 +90,7 @@ public void FormatBytesWithLimit_returns_expected_string_when_less_than_next_siz
var result = DisplayStringHelper.FormatBytesWithLimit(12, bytes);

// Then
result.Should().Be("12B / 1024MiB");
result.Should().Be("12B / 1024MB");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void AdminUsers_and_Centre_populate_expected_values()
viewModel.TrainersColour.Should().Be("green");
viewModel.CustomCourses.Should().Be("10 / 12");
viewModel.CustomCoursesColour.Should().Be("yellow");
viewModel.ServerSpace.Should().Be("1KiB / 1GiB");
viewModel.ServerSpace.Should().Be("1KB / 1GB");
viewModel.ServerSpaceColour.Should().Be("green");
}

Expand Down
6 changes: 4 additions & 2 deletions DigitalLearningSolutions.Web/Helpers/DisplayStringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public static class DisplayStringHelper
{
private const string Divider = " / ";
private static readonly string[] Units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
private static readonly string[] Units = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };

public static string FormatNumberWithLimit(int number, int limit)
{
Expand All @@ -30,7 +30,9 @@ private static string GenerateBytesDisplayString(long byteCount)
}

var place = Convert.ToInt32(Math.Floor(Math.Log(byteCount, 1024)));
var number = Math.Round(byteCount / Math.Pow(1024, place), 1);
// Do not include decimal place below GB
var decimalPlaces = place <= 2 ? 0 : 1;
var number = Math.Round(byteCount / Math.Pow(1024, place), decimalPlaces);
return (Math.Sign(byteCount) * number) + Units[place];
}
}
Expand Down