diff --git a/UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs b/UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs index f2502153d5..c07c02af5f 100644 --- a/UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs +++ b/UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs @@ -34,6 +34,10 @@ public void FeetInchesRoundTrip() [InlineData("1′", 1)] // Feet only [InlineData("1\"", 0.08333333)] // Inches only [InlineData("1″", 0.08333333)] // Inches only + [InlineData("0' 1\"", 0.08333333)] // Inches only + [InlineData("0' 1″", 0.08333333)] // Inches only + [InlineData("0′ 1\"", 0.08333333)] // Inches only + [InlineData("0′ 1″", 0.08333333)] // Inches only [InlineData("1' 1\"", 1.08333333)] // Normal form [InlineData("1′ 1″", 1.08333333)] // Normal form [InlineData(" 1′ 1″ ", 1.08333333)] // Normal form, requires trimming @@ -45,6 +49,10 @@ public void FeetInchesRoundTrip() [InlineData("-1′", -1)] // Feet only [InlineData("-1\"", -0.08333333)] // Inches only [InlineData("-1″", -0.08333333)] // Inches only + [InlineData("-0' 1\"", -0.08333333)] // Inches only + [InlineData("-0' 1″", -0.08333333)] // Inches only + [InlineData("-0′ 1\"", -0.08333333)] // Inches only + [InlineData("-0′ 1″", -0.08333333)] // Inches only [InlineData("-1' 1\"", -1.08333333)] // Normal form [InlineData("-1′ 1″", -1.08333333)] // Normal form [InlineData(" -1′ 1″ ", -1.08333333)] // Normal form, requires trimming diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 50e6a8f296..97f64c4805 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -88,17 +88,24 @@ public static bool TryParseFeetInches([CanBeNull] string str, out Length result, string inchRegex = quantityParser.CreateRegexPatternForUnit(LengthUnit.Inch, formatProvider, matchEntireString: false); // Match entire string exactly - string pattern = $@"^(?{footRegex})\s?(?{inchRegex})$"; + string pattern = $@"^(?\-?)(?{footRegex})\s?(?{inchRegex})$"; var match = new Regex(pattern, RegexOptions.Singleline).Match(str); - if (!match.Success) return false; + if (!match.Success) + return false; + var negativeSignGroup = match.Groups["negativeSign"]; var feetGroup = match.Groups["feet"]; var inchesGroup = match.Groups["inches"]; + if (TryParse(feetGroup.Value, formatProvider, out Length feet) && TryParse(inchesGroup.Value, formatProvider, out Length inches)) { - result = feet.Value >= 0 ? feet + inches : feet - inches; + result = feet + inches; + + if(negativeSignGroup.Length > 0) + result = -result; + return true; }