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
14 changes: 14 additions & 0 deletions UnitsNet.Tests/QuantityFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,19 @@ public void Format_WithFormatParameter_FormatsWithCurrentCulture()
var actual = QuantityFormatter.Format(length, "G");
Assert.Equal(expected, actual);
}

[Theory]
[InlineData("A+0", "m")]
[InlineData("S+1", "123.3 m")]
public void Format_CustomSpecifierSuffix_ParsesUsingInvariantCulture(string format, string expected)
{
var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.PositiveSign = "!";
using var cultureScope = new CultureScope(culture);

var length = Length.FromMeters(123.321);

Assert.Equal(expected, QuantityFormatter.Default.Format(length, format));
}
}
}
16 changes: 8 additions & 8 deletions UnitsNet/QuantityFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public string Format<TQuantity>(TQuantity quantity, string? format = null, IForm
switch (format[0])
{
#if NET
case 'S' or 's' when int.TryParse(format.AsSpan(1), out var precisionSpecifier):
case 'S' or 's' when int.TryParse(format.AsSpan(1), CultureInfo.InvariantCulture, out var precisionSpecifier):
return ToStringWithSignificantDigitsAfterRadix(quantity, formatProvider, precisionSpecifier);
case 'A' or 'a' when int.TryParse(format.AsSpan(1), out var abbreviationIndex):
case 'A' or 'a' when int.TryParse(format.AsSpan(1), CultureInfo.InvariantCulture, out var abbreviationIndex):
{
IReadOnlyList<string> abbreviations = _unitAbbreviations.GetUnitAbbreviations(quantity.UnitKey, formatProvider);

Expand All @@ -157,14 +157,14 @@ public string Format<TQuantity>(TQuantity quantity, string? format = null, IForm

return abbreviations[abbreviationIndex];
}
case 'C' or 'c' when int.TryParse(format.AsSpan(1), out _):
case 'C' or 'c' when int.TryParse(format.AsSpan(1), CultureInfo.InvariantCulture, out _):
throw new FormatException($"The \"{format}\" (currency) format is not supported.");
case 'P' or 'p' when int.TryParse(format.AsSpan(1), out _):
case 'P' or 'p' when int.TryParse(format.AsSpan(1), CultureInfo.InvariantCulture, out _):
throw new FormatException($"The \"{format}\" (percent) format is not supported.");
#else
case 'S' or 's' when int.TryParse(format.Substring(1), out var precisionSpecifier):
case 'S' or 's' when int.TryParse(format.Substring(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out var precisionSpecifier):
return ToStringWithSignificantDigitsAfterRadix(quantity, formatProvider, precisionSpecifier);
case 'A' or 'a' when int.TryParse(format.Substring(1), out var abbreviationIndex):
case 'A' or 'a' when int.TryParse(format.Substring(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out var abbreviationIndex):
{
IReadOnlyList<string> abbreviations = _unitAbbreviations.GetUnitAbbreviations(quantity.UnitKey, formatProvider);

Expand All @@ -175,9 +175,9 @@ public string Format<TQuantity>(TQuantity quantity, string? format = null, IForm

return abbreviations[abbreviationIndex];
}
case 'C' or 'c' when int.TryParse(format.Substring(1), out _):
case 'C' or 'c' when int.TryParse(format.Substring(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out _):
throw new FormatException($"The \"{format}\" (currency) format is not supported.");
case 'P' or 'p' when int.TryParse(format.Substring(1), out _):
case 'P' or 'p' when int.TryParse(format.Substring(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out _):
throw new FormatException($"The \"{format}\" (percent) format is not supported.");
#endif
}
Expand Down
Loading