Skip to content

Commit

Permalink
Merge pull request #688 from Thecentury/dev
Browse files Browse the repository at this point in the history
Added an option to choose format provider for ByteSize humanizing
  • Loading branch information
Oren Novotny authored Mar 13, 2019
2 parents 59700e2 + 6a02bbc commit 665df27
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Humanizer.Tests.Shared/Bytes/ByteSizeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Humanizer.Bytes;
using System.Globalization;
using Humanizer.Bytes;
using Xunit;

namespace Humanizer.Tests.Bytes
Expand Down Expand Up @@ -72,6 +73,18 @@ public void HumanizesTerabytes(double input, string format, string expectedValue
Assert.Equal(expectedValue, input.Terabytes().Humanize(format));
}

[Theory]
[InlineData(2.1, null, "en-US", "2.1 TB")]
[InlineData(2.123, "#.#", "en-US", "2.1 TB")]
[InlineData(2.1, null, "ru-RU", "2,1 TB")]
[InlineData(2.123, "#.#", "ru-RU", "2,1 TB")]
public void HumanizeWithFormatProvider(double input, string format, string cultureName, string expectedValue)
{
var culture = new CultureInfo(cultureName);

Assert.Equal(expectedValue, input.Terabytes().Humanize(format, culture));
}

[Fact]
public void ByteGigabytes()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ namespace Humanizer.Bytes
public Humanizer.Bytes.ByteSize Subtract(Humanizer.Bytes.ByteSize bs) { }
public string ToFullWords() { }
public override string ToString() { }
public string ToString(System.IFormatProvider provider) { }
public string ToString(string format) { }
public string ToString(string format, System.IFormatProvider provider) { }
public static bool TryParse(string s, out Humanizer.Bytes.ByteSize result) { }
}
}
Expand Down Expand Up @@ -95,6 +97,8 @@ namespace Humanizer
public static Humanizer.Bytes.ByteSize Gigabytes(this double input) { }
public static Humanizer.Bytes.ByteSize Gigabytes(this long input) { }
public static string Humanize(this Humanizer.Bytes.ByteSize input, string format = null) { }
public static string Humanize(this Humanizer.Bytes.ByteSize input, System.IFormatProvider formatProvider) { }
public static string Humanize(this Humanizer.Bytes.ByteSize input, string format, System.IFormatProvider formatProvider) { }
public static Humanizer.Bytes.ByteSize Kilobytes(this byte input) { }
public static Humanizer.Bytes.ByteSize Kilobytes(this sbyte input) { }
public static Humanizer.Bytes.ByteSize Kilobytes(this short input) { }
Expand Down
30 changes: 26 additions & 4 deletions src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//THE SOFTWARE.

using System;
using System.Globalization;

namespace Humanizer.Bytes
{
Expand Down Expand Up @@ -212,18 +213,38 @@ public static ByteSize FromTerabytes(double value)
/// </summary>
public override string ToString()
{
return string.Format("{0} {1}", LargestWholeNumberValue, LargestWholeNumberSymbol);
return ToString(NumberFormatInfo.CurrentInfo);
}

public string ToString(IFormatProvider provider)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}

return string.Format("{0} {1}", LargestWholeNumberValue.ToString(provider), LargestWholeNumberSymbol);
}

public string ToString(string format)
{
return ToString(format, NumberFormatInfo.CurrentInfo);
}

public string ToString(string format, IFormatProvider provider)
{
if (format == null)
throw new ArgumentNullException(nameof(format));
if (provider == null)
throw new ArgumentNullException(nameof(provider));

if (!format.Contains("#") && !format.Contains("0"))
{
format = "0.## " + format;
}

bool has(string s) => format.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) != -1;
string output(double n) => n.ToString(format);
string output(double n) => n.ToString(format, provider);

if (has(TerabyteSymbol))
{
Expand Down Expand Up @@ -256,7 +277,7 @@ public string ToString(string format)
return output(Bits);
}

var formattedLargeWholeNumberValue = LargestWholeNumberValue.ToString(format);
var formattedLargeWholeNumberValue = LargestWholeNumberValue.ToString(format, provider);

formattedLargeWholeNumberValue = formattedLargeWholeNumberValue.Equals(string.Empty)
? "0"
Expand Down Expand Up @@ -437,8 +458,9 @@ public static bool TryParse(string s, out ByteSize result)
var found = false;

// Acquiring culture specific decimal separator
var decSep = Convert.ToChar(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

var decSep = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);

// Pick first non-digit number
for (num = 0; num < s.Length; num++)
{
Expand Down
23 changes: 23 additions & 0 deletions src/Humanizer/Bytes/ByteSizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,29 @@ public static string Humanize(this ByteSize input, string format = null)
return string.IsNullOrWhiteSpace(format) ? input.ToString() : input.ToString(format);
}

/// <summary>
/// Turns a byte quantity into human readable form, eg 2 GB
/// </summary>
/// <param name="input"></param>
/// <param name="formatProvider">The format provider to use</param>
/// <returns></returns>
public static string Humanize(this ByteSize input, IFormatProvider formatProvider)
{
return input.ToString(formatProvider);
}

/// <summary>
/// Turns a byte quantity into human readable form, eg 2 GB
/// </summary>
/// <param name="input"></param>
/// <param name="format">The string format to use</param>
/// <param name="formatProvider">The format provider to use</param>
/// <returns></returns>
public static string Humanize(this ByteSize input, string format, IFormatProvider formatProvider)
{
return string.IsNullOrWhiteSpace(format) ? input.ToString(formatProvider) : input.ToString(format, formatProvider);
}

/// <summary>
/// Turns a quantity of bytes in a given interval into a rate that can be manipulated
/// </summary>
Expand Down

0 comments on commit 665df27

Please sign in to comment.