Skip to content

Commit

Permalink
Basic implementation of Humanizr#526 with tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBarka committed Jan 3, 2019
1 parent 9ba6fc1 commit 6b83121
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
32 changes: 17 additions & 15 deletions src/Humanizer.Tests.Shared/MetricNumeralTests.cs
Expand Up @@ -90,24 +90,26 @@ public void TestAllSymbolsAsInt(int exponent)
}

[Theory]
[InlineData("0", 0d, false, true, null)]
[InlineData("123", 123d, false, true, null)]
[InlineData("-123", (-123d), false, true, null)]
[InlineData("1.23k", 1230d, false, true, null)]
[InlineData("1 k", 1000d, true, true, null)]
[InlineData("1 kilo", 1000d, true, false, null)]
[InlineData("1milli", 1E-3, false, false, null)]
[InlineData("1.23milli", 1.234E-3, false, false, 2)]
[InlineData("12.34k", 12345, false, true, 2)]
[InlineData("12k", 12345, false, true, 0)]
[InlineData("-3.9m", -3.91e-3, false, true, 1)]
public void ToMetric(string expected, double input, bool hasSpace, bool useSymbol, int? decimals)
[InlineData("0", 0d, false, true, null, null)]
[InlineData("123", 123d, false, true, null, null)]
[InlineData("-123", (-123d), false, true, null, null)]
[InlineData("1.23k", 1230d, false, true, null, null)]
[InlineData("1 k", 1000d, true, true, null, null)]
[InlineData("1 kilo", 1000d, true, false, null, null)]
[InlineData("1milli", 1E-3, false, false, null, null)]
[InlineData("1.23milli", 1.234E-3, false, false, 2, null)]
[InlineData("12.34k", 12345, false, true, 2, null)]
[InlineData("12k", 12345, false, true, 0, null)]
[InlineData("-3.9m", -3.91e-3, false, true, 1, null)]
[InlineData("-3.9m", -3.91e-3, false, true, 1, 'M')]
[InlineData("10M", 10_000_000, false, true, null, 'G')]
[InlineData("10000k", 10_000_000, false, true, null, 'k')]
[InlineData("1234.56k", 1_234_560, false, true, null, 'k')]
public void ToMetric(string expected, double input, bool hasSpace, bool useSymbol, int? decimals, char? largestPrefix)
{
Assert.Equal(expected, input.ToMetric(hasSpace, useSymbol, decimals));
Assert.Equal(expected, input.ToMetric(hasSpace, useSymbol, decimals, largestPrefix));
}



[Theory]
[InlineData(1E+27)]
[InlineData(1E-27)]
Expand Down
18 changes: 12 additions & 6 deletions src/Humanizer/MetricNumeralExtensions.cs
Expand Up @@ -243,34 +243,40 @@ private static string BuildRepresentation(double input, bool hasSpace, bool useS
/// <returns>A number in a Metric representation</returns>
private static string BuildMetricRepresentation(double input, int exponent, bool hasSpace, bool useSymbol, int? decimals, char? largestPrefix = null)
{
if (largestPrefix != null)
if (largestPrefix.HasValue)
exponent = LimitExponent(exponent, (char)largestPrefix);

var number = input * Math.Pow(1000, -exponent);

if (decimals.HasValue)
{
number = Math.Round(number, decimals.Value);
}

var symbol = Math.Sign(exponent) == 1
? Symbols[0][exponent - 1]
: Symbols[1][-exponent - 1];

return number
+ (hasSpace ? " " : string.Empty)
+ GetUnit(symbol, useSymbol);
}

// TODO docs
/// <summary>
/// Limit upper size of exponent value to that corresponding to a metric prefix symbol. TODO synbol or name.
/// </summary>
/// <param name="exponent">The exponent to limit.</param>
/// <param name="useSymbol">True will use symbol instead of name</param>
/// <returns>A symbol or a symbol's name</returns>
private static int LimitExponent(int exponent, char largestPrefix)
{
// TODO largestPrefix = largestPrefix.Trim(); When changed to string

if (!(Symbols[0].Contains(largestPrefix) || Symbols[1].Contains(largestPrefix)))
throw new ArgumentException("Empty or invalid Metric prefix character.", nameof(largestPrefix)); // TODO change to "string".

return exponent; // TODO
}
var maxExponent = (Symbols[0].IndexOf(largestPrefix) + 1); // TODO check symbols[1]

return maxExponent < exponent ? maxExponent : exponent;
}

/// <summary>
/// Get the unit from a symbol of from the symbol's name.
Expand Down

0 comments on commit 6b83121

Please sign in to comment.