Skip to content
Open
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
46 changes: 41 additions & 5 deletions src/Humanizer/MetricNumeralExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ static string CleanRepresentation(string input)
ArgumentNullException.ThrowIfNull(input);

input = input.Trim();
input = ReplaceNameBySymbol(input);
if (input.Length > 0 && char.IsLetter(input[^1]) && !HasSymbolSuffix(input))
{
input = ReplaceNameBySymbol(input);
}

if (input.Length == 0 || input.IsInvalidMetricNumeral())
{
throw new ArgumentException("Empty or invalid Metric string.", nameof(input));
Expand All @@ -264,6 +268,10 @@ static string CleanRepresentation(string input)
return input.Replace(" ", string.Empty);
}

static bool HasSymbolSuffix(string input) =>
UnitPrefixes.ContainsKey(input[^1]) &&
(input.Length == 1 || !char.IsLetter(input[^2]));

/// <summary>
/// Build a number from a metric representation or from a number
/// </summary>
Expand Down Expand Up @@ -303,9 +311,15 @@ static double BuildMetricNumber(string input, char last)
/// </summary>
/// <param name="input">Metric representation with a name or a symbol</param>
/// <returns>A metric representation with a symbol</returns>
static string ReplaceNameBySymbol(string input) =>
UnitPrefixes.Aggregate(input, (current, unitPrefix) =>
current.Replace(unitPrefix.Value.Name, unitPrefix.Key.ToString()));
static string ReplaceNameBySymbol(string input)
{
foreach (var unitPrefix in UnitPrefixes)
{
input = input.Replace(unitPrefix.Value.Name, GetSymbolText(unitPrefix.Key));
}

return input;
}

/// <summary>
/// Build a Metric representation of the number.
Expand Down Expand Up @@ -496,9 +510,31 @@ static string GetUnitText(char symbol, MetricNumeralFormats? formats)
}
}

return symbol.ToString();
return GetSymbolText(symbol);
}

static string GetSymbolText(char symbol) =>
symbol switch
{
'k' => "k",
'M' => "M",
'G' => "G",
'T' => "T",
'P' => "P",
'E' => "E",
'Z' => "Z",
'Y' => "Y",
'm' => "m",
'μ' => "μ",
'n' => "n",
'p' => "p",
'f' => "f",
'a' => "a",
'z' => "z",
'y' => "y",
_ => symbol.ToString()
};

/// <summary>
/// Check if a Metric representation is out of the valid range.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions tests/Humanizer.Tests/MetricNumeralTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public class MetricNumeralTests
public void FromMetric(double expected, string input) =>
Assert.Equal(expected, input.FromMetric());

[Theory]
[InlineData(1E6, "1 mega")]
[InlineData(1E-18, "1a")]
[InlineData(1E-18, "1 a")]
public void FromMetricDistinguishesNamesFromSymbolSuffixes(double expected, string input) =>
Assert.Equal(expected, input.FromMetric());

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down