diff --git a/src/Humanizer.Tests.Shared/MetricNumeralTests.cs b/src/Humanizer.Tests.Shared/MetricNumeralTests.cs index 252a753d2..9d719fe14 100644 --- a/src/Humanizer.Tests.Shared/MetricNumeralTests.cs +++ b/src/Humanizer.Tests.Shared/MetricNumeralTests.cs @@ -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)] diff --git a/src/Humanizer/MetricNumeralExtensions.cs b/src/Humanizer/MetricNumeralExtensions.cs index bb1fa37a1..34635dc13 100644 --- a/src/Humanizer/MetricNumeralExtensions.cs +++ b/src/Humanizer/MetricNumeralExtensions.cs @@ -243,24 +243,29 @@ private static string BuildRepresentation(double input, bool hasSpace, bool useS /// A number in a Metric representation 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 + /// + /// Limit upper size of exponent value to that corresponding to a metric prefix symbol. TODO synbol or name. + /// + /// The exponent to limit. + /// True will use symbol instead of name + /// A symbol or a symbol's name private static int LimitExponent(int exponent, char largestPrefix) { // TODO largestPrefix = largestPrefix.Trim(); When changed to string @@ -268,9 +273,10 @@ private static int LimitExponent(int exponent, char largestPrefix) 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; + } /// /// Get the unit from a symbol of from the symbol's name.