Skip to content

Best approach? #273

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
poostwoud opened this issue Aug 21, 2017 · 8 comments
Closed

Best approach? #273

poostwoud opened this issue Aug 21, 2017 · 8 comments

Comments

@poostwoud
Copy link

Hi,

First of all, compliments on your work! Really cool stuff.

I would like to add a "Pieces" unit, where piece is defined as a quantity of something, e.g. 18 pieces of a laptop.

Now the hard part is that 18 pieces should actually be read as 18 x 1 piece, where 1 piece is the unit. But I would also like to be able to say that it's 1 x 18 pieces. This would mean making the factor versus the baseunit a variable.

Is something like this possible with UnitsNet? What would be the best approach for supporting this?

Please let me know if you need more information.

Cheers,

Peter

@angularsen
Copy link
Owner

Thanks! Happy to hear you enjoy it.
We have already touched on this topic here: #181

Could you read through it and comment on whether that answers your question or if you still see a use for adding this to the library? I'm all open to it, I just need a good, specific usecase to relate to where adding it brings real value.

@poostwoud
Copy link
Author

Hi Andreas,

Missed that topic. Interesting! You're writing:

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.

I believe there is no real difference between "piece" and "meter" in that aspect, only the way the factor of the unit is used. Consider the following:

1 kilometer = 1000 meter

You can actually read a multiplication operator in both. Let us take "1000 meter" for example. You could write this as:

1000 x 1 meter

In this example the unit is "1 meter", but what if I would write it like this:

1 x 1000 meter

Then "1000 meter" would be the unit. Now because of the SI, we write "1000 meter" like "kilometer", resulting in:

1 x 1 kilometer

So the first equation can also be written as:

1 x 1 kilometer = 1000 x 1 meter = 1 x 1000 meter

Usually we don't write it like this, because it's awkward, however that is exactly how pieces work. Usually the SI prefices are not used (kilo, deca, etc.), but the way of work is similar:

1000 x 1 piece = 1 x 1000 pieces = 4 x 250 pieces, etc.

Where "1 piece", "1000 pieces" and "250 pieces" are actually units. In trade they can be used as sales unit, purchase unit, etc.

Curious to hear what you think of this!

Cheers,

Peter

@angularsen
Copy link
Owner

Oh, you closed it? I haven't had time to look at this yet.

@poostwoud
Copy link
Author

Hmmm... Not what i wanted to do, sorry, I dont know how I did this...

@eriove eriove reopened this Aug 24, 2017
@angularsen
Copy link
Owner

I get what you are saying about 1km = 1 x 1000 m = 1000 x 1m, however, 1 m and 1 km are SI standard units while 1000 m is not.

The best way this would translate into UnitsNet's current representation, is to add a new quantity Amount with units Single, Double, Triple, Dozen, Gross, SmallGross, GreatGross and probably a bunch of other cases. These are actual standardized units, that can be converted between.

https://en.wikipedia.org/wiki/Category:Units_of_amount

I don't see a good way to introduce what you are proposing, with M x N pieces, without creating a separate type for this. Something like:

public struct AmountMxN
{
    public AmountMxN(double m, double n, string name)
   { 
       M = m;
       N = n;
       Name = name;
    }
    public double M { get; }
    public double N { get; }
    public string Name {get;}
    public double Amount => M * N;
    public double ToM(double m) => new AmountMxN(m, Amount / m, Name);
    public double ToN(double n) => new AmountMxN(Amount / n, n, Name);
}

var laptops1 = new AmountMxN(1, 18, "laptop"); // 1x18 laptops
var laptops2 = laptops1.AsM(2); // 2x9 laptops

But this does not feel right for a type to belong in UnitsNet. It seems like application-specific code, rather than generic unit representations and conversions. If I have misunderstood or you have a better proposal, please let me know.

So what do you think of Amount and my understanding of the MxN implementation?

@angularsen
Copy link
Owner

Any more thoughts on this?

@poostwoud
Copy link
Author

Sorry for the late response! The Amount approach is interesting (never heard of the unit Megadeath before...), but as the units can be indeed specific for the business I think your MxN implementation could work... I will experiment with this and let you know how it works. I might take some time as other projects are taking my attention right know. Thanks so far! Cheers.

@angularsen
Copy link
Owner

Alright, I'm closing this topic for now then. Please reopen if you want to continue the discussion at some point.

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

3 participants