UnitsNet
UnitsNet copied to clipboard
Custom units incompatible with decimal format?
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 - https://github.com/angularsen/UnitsNet/pull/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.
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 (typicallydouble
) - Or it gets
double
from methods likeAs(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.
So it is impossible to use e.g. a Length quantity that stores a decimal as an internal value?
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