Skip to content

Unit: Piece or Amount missing #181

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
andreasfrey opened this issue Aug 31, 2016 · 8 comments
Closed

Unit: Piece or Amount missing #181

andreasfrey opened this issue Aug 31, 2016 · 8 comments

Comments

@andreasfrey
Copy link

Hello,

How can I use UnitsNet lib for a concrete amount of things.

UseCase: Shopping Cart

  • 4 kg works
  • 4 pieces does not work

Is there a way to do this?

@angularsen
Copy link
Owner

Hi, there is no unit that work with the concept of "piece" or "amount", because I think fundamentally these are not units - you don't convert between them. They are simply amounts, or numeric.

If you could post some code that would provide some context, that may be helpful to understand why we possibly want to add a piece unit.

@angularsen
Copy link
Owner

angularsen commented Aug 31, 2016

The closest unit is RatioUnit.DecimalFraction, but it does not have any abbrevations.

https://github.com/anjdreas/UnitsNet/blob/master/UnitsNet/GeneratedCode/UnitClasses/Ratio.g.cs

@andreasfrey
Copy link
Author

andreasfrey commented Aug 31, 2016

Hi,

I want to realize something like a shopping cart functionality.

Imagine I can buy flour and glasses. Both have articlenumbers, I want to offer flour based on weight, and glasses per piece. I plan to add generic articles in the Shopping cart.

pseudo-code
Article {
"Product": "Articlenumber",
"Quantity": "UOM"
}

I hope this shows the problem.

Is this doable with UnitNet?
Is there a base type for all Units within UnitsNet?

@andreasfrey
Copy link
Author

Hi,

to be more precise, I need something like this:

UoM quantity1 = Amount.Value(5); // assumption unit is pieces - the unit must not have a name
UoM quantity2 = Length.FromMeters(15);
UoM quantity3 = Mass.FromKgs(17);

UoM should then contain:

  • Type: Amount | Mass | Length
  • Value: 5 | 15 | 17
  • Unit: "" | m | kg

No special calculations for Amount (+ - * /)
UoM supports the calculation rules of the underlying types. It is not possible to calculate with different UoM types.

@angularsen
Copy link
Owner

I really think this is best solved outside Units.NET on a per-application basis with a wrapper type to store the meta-information.

  1. We can't expose operator overloads +-/* on a common interface, because it would just throw exceptions for values of incompatible units and I don't think that would be a good design.
  2. You can retrieve the unit type by Object.GetType() or is operator.
  3. Length etc. does not contain the unit you constructed it with, it only contains the length representation. If you need to know the unit you created it with, I think that is best solved in a wrapper type like your UoM type.

So for a shopping list, I would create a list of ShopListItem objects with maybe a string Name and AmountInfo Amount property, where the latter is similar to what you describe with UoM above, except storing string Unit and object Value.

If I needed to get the sum of liters of milk and there were two items of 0.5L and 1L each, I would probably do something like double sumLitersOfMilk = items.Where(x => x.Name == "milk").Select(x => x.Amount.Value).OfType<Volume>().Select(x => x.Liters).Sum().
That way you reuse the capabilities of Units.NET for expressing statically typed units and values.

Another option is to let AmountInfo, or your UoM type do the work, and let Sum() work on the Value property of items with Type == Volume. A lot of ways to skin this cat, but ultimately I don't see that Units.NET can provide this. Correct me if I am mistaken.

@angularsen
Copy link
Owner

Do you have any further comments here?

@ghost
Copy link

ghost commented Nov 8, 2016

No, fine for me thanks.

I will find another way then.

@angularsen
Copy link
Owner

Ok, closing this until there is a more clear usecase for bringing this into the lib.

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

No branches or pull requests

2 participants