Skip to content

How to implement fractions of inches #676

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

Closed
z93blom opened this issue Jul 11, 2019 · 3 comments
Closed

How to implement fractions of inches #676

z93blom opened this issue Jul 11, 2019 · 3 comments

Comments

@z93blom
Copy link

z93blom commented Jul 11, 2019

We have just started using UnitsNet in our applications for the building industry. In the US it is customary to use FeetInches to present lengths of timber, but the resolution of a single inch is too big in many cases, and therefore they are used to presenting the remainder as a fractional part (x/2, y/4, z/16...). An example:

3 feet 3 11/32 inches
which is almost equal to 1 m

Traditionally I would implement this as a custom formatter where I could specify the max precision (full inches, half, quarter, eighths, sixteenth, 1/32, 1/64 would be the most obvious choices).

However I do not know if this should be thought of as a separate LengthUnit in UnitsNet. I see that there are support for formatters as well, so please guide me as to where this would be a good fit.

Right now I do not need to parse these units, only present them, but there might be others out there who might need to parse this. I assume this might be the fact when for all any units that are using inches (Inch, FeetInches).

Are there any suggestions for how/where this functionality would fit in?

Related issues are:
#579 - Enhanced IFormattable/ToString Support
#585 - Angle (Degrees) to N-S-E-W

Potential gotchas (when implementing):
#673 - Parsing negative FeetInches values

@angularsen
Copy link
Owner

Hi, I was on vacation and not able to reply before now.

However I do not know if this should be thought of as a separate LengthUnit in UnitsNet.

No, I don't consider this a new unit. FeetInches is not a separate unit either, it's only a special case we added to Length via Length.ParseFeetInches(), Length.FeetInches and FeetInches.ToString() since the concept is so widely used.

Traditionally I would implement this as a custom formatter where I could specify the max precision (full inches, half, quarter, eighths, sixteenth, 1/32, 1/64 would be the most obvious choices).

Yes, I think that is a good way to go about it. If you are able to make it generic and reusable, then we could put it into this repo for others to use.

If this is for WPF, I believe you can use IValueConverter as discussed in #290. Then you can have two-way conversion from value to text and from text to value (parsing).

We also have a sample app in https://github.com/angularsen/UnitsNet/tree/master/Samples/WpfMVVMSample that maybe does something similar to what you want.

Did that help answer your questions?

@z93blom
Copy link
Author

z93blom commented Jul 16, 2019

Please don't apologize for not replying immediately. It's not even been a week! Thank you for taking your time to answer my questions.
After studying how the library worked a bit more I came up with the following solution:
I implemented the whole thing as a custom formatter for Length, as well as an extension method to Length to get the output.
This worked fine for me since I have no need to parse the input (I'm forcing all settings to be entered in mm - even for the Americans).

The formatter has an option for the precision to be used (from full inches only down to x/64th).

It might be useable for others as well. I'll see what I can do about cleaning it up and asking for permission to publish it (it was developed as a commision so I do not currently own the copyright).

You can go ahead and close this question.

@z93blom z93blom closed this as completed Jul 16, 2019
@angularsen
Copy link
Owner

Great! I'm glad you figured out a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants