-
Notifications
You must be signed in to change notification settings - Fork 393
Parsing negative FeetInches values #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi and thanks for a detailed report, very helpful. My first hunch is that you've run into an edge case we simply didn't think of. Parsing feet-inches has a special implementation to support parsing two parts, and I guess it doesn't recognize the minus for both parts. If you would be interested in trying to fix the bug and write a new unit test case for this scenario, then I'm happy to assist on getting a pull request merged. I'm currently on vacation and the other collaborators have been busy lately too, so it can take some time to find the time to look closer at this. |
@deanwiles this has been fixed in 4.27.0 |
Thank you @angularsen and @tmilnthorp for such a fast response. Unfortunately, there are two related issues with the fix.
The following code handles that case, but you may have a better solution.
|
Indeed that case was missed! I have created a fix in PR #675 As for the double negatives, I believe that's a fine assumption, but maybe not. Would -2 feet -6 inches be the same as -1 foot, 6 inches? Or should we always assume inches to be negative? @angularsen That's a different issue/PR scope though |
Good catch, and good fix. Merged. As for double negatives, I'm not quite sure what my intuition expects. So something tells me that negative feet/inches is not widely used and there is no single convention on what is right here.
|
Thanks, @tmilnthorp for the fix in PR #675; much cleaner than my suggestion. However, if a double negative occurs, it does not yield the expected result (e.g. -1'-2" => -(1'-2") => -0'10" instead of -1'2").
This can be fixed by changing the line that combines feet and inches:
Thanks, @angularsen for the online examples. I think the answer lies in whether FeetInches is a unit or the sum of two parts? IMHO, FeetInches should be a unit and just parse and output a single unary negative sign (e.g. -1'2", -0'2", -2"). Negative decimal numbers and their fractional parts could be used for precedence. There are differing views of whether the fractional part can be negative, but the decimal number as a whole just has one leading negative sign if < 0. A way to implement this would be to add a Sign property to FeetInches:
Associated tests:
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Describe the bug
Unexpected behavior from Length.ParseFeetInches when parsing negative coordinates.
We have an application that imports values on the x- and y-axes relative to a home position. A position that is 11 ft 2 in from the left of center has an x value of -11'2". Note that the negative sign applies to both feet and inches, essentially meaning -(11'2"). Unfortunately Length.ParseFeetInches evaluates -11'2" as -10'-10" (effectively -11' plus 2").
It appears that Length.ParseFeetInches is expecting two negative signs when parsing negative values (e.g. -11'-2"). Is that the correct behavior? I've never seen multiple negatives in coordinates, but I also couldn't find any defined standard for this online.
To Reproduce
Samples from Visual Studio Debugger Immediate Window:
?Length.ParseFeetInches("-11'2"").FeetInches.ToString()
"-10 ft -10 in"
?Length.ParseFeetInches("-11 ft 2 in").FeetInches.ToString()
"-10 ft -10 in"
?Length.ParseFeetInches("-11'-2"").FeetInches.ToString()
"-11 ft -2 in"
?Length.ParseFeetInches("-11 ft -2 in").FeetInches.ToString()
"-11 ft -2 in"
?Length.ParseFeetInches("-(11'2")").FeetInches.ToString()
throws 'System.FormatException'
Expected behavior
I expected that the leading negative sign would apply to the entire unit (e.g. -11'2" = -(11'2")).
Comments
I really like UnitsNet! Very robust and flexible. (Just wasn't expecting this behavior.)
It doesn't appear that Length.ParseFeetInches handles parentheses, so if the current behavior is correct, I may have to detect and strip off leading negatives and then multiply by -1 after parsing.
Thanks, Dean.
The text was updated successfully, but these errors were encountered: