Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

More complete sample of custom units #1189

Unanswered
MisinformedDNA asked this question in Q&A
Discussion options

I'm looking for a more complete sample of a custom units. I've been looking at this, and I have a class that works fine in isolation, but is I don't see how it integrates with the rest of the system, like using UnitConverter or abbreviations.

You must be logged in to vote

Replies: 2 comments 4 replies

Comment options

Custom units is the least mature part of the library. It is mostly exploratory to see what we can do with it, in particular for those that want to add units that we don't accept in the main library.

I see the wiki doesn't really explain why one would add a custom unit, and to be honest, the benefits are small in its current state.

You lose out on what I think is the biggest benefit of the library, such as generating quantity types with factory method FromMeters() and conversion properties Meters. C# source generators might provide something like that.

Key benefits

As I recall at least.

  • Reuse functionality that operates on IQuantity
    • Dynamically convert to unit with .As(Enum)
    • UnitMath with min, max, sum, average etc.
    • .NET generic math, currently Sum() and Average()
  • Reuse UnitConverter to dynamically convert between units by their enum values
    • Also allows you to dynamically convert between your custom units and the built-in units, such as CustomLengthUnit.ElbowToThumb to LengthUnit.Meter.
  • Reuse QuantityParser and UnitParser to parse quantity strings like "5 cm" and "cm" for your own quantities and units

There are probably more I'm forgetting.

What could be better

I'm adding this to the wiki.

Did that answer your question?

It would also be helpful to know;
What did you expect the integration to do?
Anything we can add to make it better?

You must be logged in to vote
2 replies
Comment options

I need to sum up all quantities for an order. Most of the quantities are expressed in weight, but some are expressed in "case" or "each". I didn't see a way to add a unit to MassUnit, so I created a new unit OrderQuantityUnit and a new quantity implementation OrderQuantity.

If I now use UnitMath.Sum(), I get the error:

Unit value Pound of type SharedKernel.ValueObjects.OrderQuantityUnit is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a third-party enum type defined outside UnitsNet library?

It makes sense, but I also don't see how to make it work.

Comment options

You can check the implementation of UnitMath.Sum():

https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/UnitMath.cs#L33-L37

 public static TQuantity Sum<TQuantity>(this IEnumerable<TQuantity> source, Enum unitType)
 where TQuantity : IQuantity
 {
 return (TQuantity) Quantity.From(source.Sum(x => x.As(unitType)), unitType);
 }

As you can see, it basically calls As(unitType) on each IQuantity you pass in.
It then calls Quantity.From(double, Enum) to construct the quantity.
This is where it fails, this part is still hard-coded to just the built-in quantities.

https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/GeneratedCode/Quantity.g.cs#L297

We could make this extensible, so you can plug in your own quantities and then Sum() should work.
As mentioned earlier, custom units is not very mature yet.

If you are interested in attempting a pull request, I'm happy to assist and provide pointers on how it could be extended.
I don't have much spare time to spend on this project myself these days.

Comment options

This is 1,5 years old. Was there any progress?
I need to calculate different H profiles and tubes to kg for example.

You must be logged in to vote
2 replies
Comment options

Not much new since then I think, v6 is working on simplifying the IQuantity interface and relying more on extension methods, other than that I believe the above summary still applies.

Comment options

lipchev Sep 30, 2025
Collaborator

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

AltStyle によって変換されたページ (->オリジナル) /