2

I would like to be able to do such things as

var m1 = new UnitOfMeasureQuantityPair(123.00, UnitOfMeasure.Pounds);
var m2 = new UnitOfMeasureQuantityPair(123.00, UnitOfMeasure.Liters);

m1.ToKilograms();
m2.ToPounds(new Density(7.0, DensityType.PoundsPerGallon);

If there isn't something like this already, anybody interested in doing it as an os project?

Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • I've run into these before as custom jobs. I think it's tough because you need so much context (US vs. UK, volume vs. mass vs. liquid vs. density etc. to do conversions properly. – Michael Haren Sep 10 '08 at 13:37

6 Answers6

4

Check out the Measurement Unit Conversion Library on The Code Project.

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
2

We actually built one in-house where I work. Unfortunately, it's not available for the public.

This is actually a great project to work on and it's not that hard to do. If you plan on doing something by yourself, I suggest you read about Quantity, Dimension and Unit (fundamental units).

These helped us understand the domain of the problem clearly and helped a lot in designing the library.

mbillard
  • 38,386
  • 18
  • 74
  • 98
2

In Chapter 10. Quantity archetype pattern of the book Enterprise Patterns and MDA: Building Better Software with Archetype Patterns and UML by Jim Arlow and Ila Neustadt there is a really useful discussion of this topic and some general patterns you could use as a guide.

Greg
  • 404
  • 4
  • 15
rohancragg
  • 5,030
  • 5
  • 36
  • 47
1

There is an (old) article on CodeProject. I've used it in a production environment previously and it worked great. We had some minor issues (performance amongst others), which I addressed. I put all this in a library you can find here.

Disclaimer: I am the maintainer of this project, so this might be conceived as a shameless plug. The library is free (as in beer and as in speech) however.

It includes the SI units, but also allows creating new units and conversions.

So you can for example create a unit "XP" (experience points). You can then register a conversion to "m" (meter, makes no sense, but you can do it). You can also create an amount like 3 XP/min (3 experience points per minute). I believe it offers decent defaults, while allowing flexibility.

Peter
  • 13,733
  • 11
  • 75
  • 122
1

Also see the most recent F# release - it has static measurement domain/dimension analysis.

  • reeeehhhheeeheeallly? I've just been itching to use F#. – George Mauer Sep 10 '08 at 14:22
  • 1
    Just a warning, F# units are meant for static checking at compile time. There is no way to inspect or set units at runtime. https://fsharpforfunandprofit.com/posts/units-of-measure/#units-of-measure-at-runtime – Greg May 06 '19 at 16:35
0

Unix units is imho brilliant; source must be on the web somewhere.
(Under "bugs", the original doc said "do not base your financial plans on the currency conversions".)

denis
  • 21,378
  • 10
  • 65
  • 88