Skip to content

Commit 3ebfbe6

Browse files
tmilnthorpangularsen
authored andcommitted
Fix for negative feet/inch parsing (#674)
* Fix for negative feet/inch parsing
1 parent 94c4a6a commit 3ebfbe6

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

UnitsNet.Tests/CustomCode/LengthTests.FeetInches.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ public void FeetInchesRoundTrip()
4141
[InlineData("1′1″", 1.08333333)] // Without space
4242
[InlineData("1 ft 1 in", 1.08333333)]
4343
[InlineData("1ft 1in", 1.08333333)]
44+
[InlineData("-1'", -1)] // Feet only
45+
[InlineData("-1′", -1)] // Feet only
46+
[InlineData("-1\"", -0.08333333)] // Inches only
47+
[InlineData("-1″", -0.08333333)] // Inches only
48+
[InlineData("-1' 1\"", -1.08333333)] // Normal form
49+
[InlineData("-1′ 1″", -1.08333333)] // Normal form
50+
[InlineData(" -1′ 1″ ", -1.08333333)] // Normal form, requires trimming
51+
[InlineData("-1'1\"", -1.08333333)] // Without space
52+
[InlineData("-1′1″", -1.08333333)] // Without space
53+
[InlineData("-1 ft 1 in", -1.08333333)]
54+
[InlineData("-1ft 1in", -1.08333333)]
4455
public void TryParseFeetInches(string str, double expectedFeet)
4556
{
4657
Assert.True(Length.TryParseFeetInches(str, out Length result));

UnitsNet/CustomCode/Quantities/Length.extra.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static bool TryParseFeetInches([CanBeNull] string str, out Length result,
9898
if (TryParse(feetGroup.Value, formatProvider, out Length feet) &&
9999
TryParse(inchesGroup.Value, formatProvider, out Length inches))
100100
{
101-
result = feet + inches;
101+
result = feet.Value >= 0 ? feet + inches : feet - inches;
102102
return true;
103103
}
104104

0 commit comments

Comments
 (0)