Skip to content

Commit 467f992

Browse files
tmilnthorpangularsen
authored andcommitted
Fix for negative zero with feet inches parsing. Added tests (#675)
1 parent aeb64b8 commit 467f992

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void FeetInchesRoundTrip()
3434
[InlineData("1′", 1)] // Feet only
3535
[InlineData("1\"", 0.08333333)] // Inches only
3636
[InlineData("1″", 0.08333333)] // Inches only
37+
[InlineData("0' 1\"", 0.08333333)] // Inches only
38+
[InlineData("0' 1″", 0.08333333)] // Inches only
39+
[InlineData("0′ 1\"", 0.08333333)] // Inches only
40+
[InlineData("0′ 1″", 0.08333333)] // Inches only
3741
[InlineData("1' 1\"", 1.08333333)] // Normal form
3842
[InlineData("1′ 1″", 1.08333333)] // Normal form
3943
[InlineData(" 1′ 1″ ", 1.08333333)] // Normal form, requires trimming
@@ -45,6 +49,10 @@ public void FeetInchesRoundTrip()
4549
[InlineData("-1′", -1)] // Feet only
4650
[InlineData("-1\"", -0.08333333)] // Inches only
4751
[InlineData("-1″", -0.08333333)] // Inches only
52+
[InlineData("-0' 1\"", -0.08333333)] // Inches only
53+
[InlineData("-0' 1″", -0.08333333)] // Inches only
54+
[InlineData("-0′ 1\"", -0.08333333)] // Inches only
55+
[InlineData("-0′ 1″", -0.08333333)] // Inches only
4856
[InlineData("-1' 1\"", -1.08333333)] // Normal form
4957
[InlineData("-1′ 1″", -1.08333333)] // Normal form
5058
[InlineData(" -1′ 1″ ", -1.08333333)] // Normal form, requires trimming

UnitsNet/CustomCode/Quantities/Length.extra.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,24 @@ public static bool TryParseFeetInches([CanBeNull] string str, out Length result,
8888
string inchRegex = quantityParser.CreateRegexPatternForUnit(LengthUnit.Inch, formatProvider, matchEntireString: false);
8989

9090
// Match entire string exactly
91-
string pattern = $@"^(?<feet>{footRegex})\s?(?<inches>{inchRegex})$";
91+
string pattern = $@"^(?<negativeSign>\-?)(?<feet>{footRegex})\s?(?<inches>{inchRegex})$";
9292

9393
var match = new Regex(pattern, RegexOptions.Singleline).Match(str);
94-
if (!match.Success) return false;
94+
if (!match.Success)
95+
return false;
9596

97+
var negativeSignGroup = match.Groups["negativeSign"];
9698
var feetGroup = match.Groups["feet"];
9799
var inchesGroup = match.Groups["inches"];
100+
98101
if (TryParse(feetGroup.Value, formatProvider, out Length feet) &&
99102
TryParse(inchesGroup.Value, formatProvider, out Length inches))
100103
{
101-
result = feet.Value >= 0 ? feet + inches : feet - inches;
104+
result = feet + inches;
105+
106+
if(negativeSignGroup.Length > 0)
107+
result = -result;
108+
102109
return true;
103110
}
104111

0 commit comments

Comments
 (0)