Skip to content
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

Custom units incompatible with decimal format? #1324

Open
marc-weber1 opened this issue Oct 25, 2023 · 3 comments
Open

Custom units incompatible with decimal format? #1324

marc-weber1 opened this issue Oct 25, 2023 · 3 comments

Comments

@marc-weber1
Copy link

Is your feature request related to a problem? Please describe.
I am trying to make a new quantity called RelativeDensity inheriting from IQuantity, but I need the underlying value to be full 128-bit decimal for precision reasons. From this pull request - #1084 - I was under the impression that QuantityValue, not double, was returned by types in UnitsNet.

Describe the solution you'd like
Add support for generic or just decimal custom quantities, and update the documentation - https://github.com/angularsen/UnitsNet/wiki/Extending-with-Custom-Units

Describe alternatives you've considered
I could try just submitting a pull request with a RelativeDensity quantity, since it just uses the same units as Density - but I have other quantities to add as well.

@angularsen
Copy link
Owner

I'm not entirely sure what you are asking for.

Are you not able to use decimal in RelativeDensity?
You can implement IValueQuantity<decimal> to expose decimal output values, similar to how Power, BitRate and Information does and use decimal as your internal value representation.

However, you also need to implement IQuantity, and any code working with IQuantity instead of IValueQuantity<decimal> gets either

  • QuantityValue wrapping whatever internal value the quantity uses (typically double)
  • Or it gets double from methods like As(Enum unit).

I know it is confusing, but generally we use double. Only the 3 mentioned quantities use decimal.
Also, custom units is kind of experimental and has a lot of rough edges like this.

@marc-weber1
Copy link
Author

So it is impossible to use e.g. a Length quantity that stores a decimal as an internal value?

@angularsen
Copy link
Owner

Yes, currently not possible to change numeric type of existing quantities, only custom quantities.

We have discussed supporting generics many times before, but I don't see that moving forward anytime soon, so if you need decimal your best option is to fork and build your own version of UnitsNet at this time.

Some related discussions:
#984
#1194
#666

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