简体   繁体   中英

c# unit conversion library for food amounts

I want to implement a simple unit conversion library for food measurements, ie cups teaspoons. pinch, milliliters, ounces, liters, grams, pounds etc etc etc. Are there any libraries out there that I can use allready, if not I want to roll my own in the pseudo manner below:

enum Unit
{
    Centimeters = 0,
    Meter = 1,
    Kilometer = 2           
}


//| |           |   0       |  1    |   2       |
//----------------------------------------------
//| |           |Centimeters| Meters| Kilometers|
//----------------------------------------------
//|0|Centimeters|1        | 0.01  | 0.000001  |
//----------------------------------------------
//|1|Meters  |100        | 1      | 1000     |
//----------------------------------------------
//|2|Kilometers |100000     | 1000  | 1         |
//----------------------------------------------


public float Convert(Unit UnitFrom, Unit UnitTo, UnitValue)
{
   float factor = UnitMatrix[UnitFrom][Unit UnitTo];
   return UnitValue * factor;
}

//Usage
Convert(Unit.Kilometers, Unit.Meters, 5)
// Lookup factor in this case would be the one at [2, 1] i.e. 1000 so output is 5000

Pointers, pitfalls, too naive? Any help would be useful. A current opensource implementation that I can study would be great too. TIA

@n4rzul, I have started developing an open-source units-of-measurement library in C#, that could easily be extended with any unit of your request. Please have a look at the project website at Github, https://github.com/cureos/csunits , and see if this library meets your needs.

I recently released Units.NET on GitHub and on NuGet .

It gives you all the common units and conversions. It is light-weight, unit tested and supports PCL.

Although I haven't incorporated all the food units yet, it is on the list of things to add.

Example conversions:

Length meter = Length.FromMeters(1);
double cm = meter.Centimeters; // 100
double yards = meter.Yards; // 1.09361
double feet = meter.Feet; // 3.28084
double inches = meter.Inches; // 39.3701

Parse and Get Culture-Specific Abbreviations:

var us = new CultureInfo("en-US");
var norwegian = new CultureInfo("nb-NO");

Unit.Tablespoon == UnitSystem.Create(us).Parse("tbsp")
Unit.Tablespoon == UnitSystem.Create(norwegian).Parse("ss")  

"T" == UnitSystem.GetDefaultAbbreviation(Unit.Tablespoon, us)
"ss" == UnitSystem.GetDefaultAbbreviation(Unit.Tablespoon, norwegian)

我刚才写了这篇文章http://www.carbonatethis.com/archives/38

Don't know if it will meet all your needs but this looks to cover a good range of measurements:

http://unitconvertlibrary.codeplex.com/

Though it looks to be in development, and there is only one class available in the source repository

I have also developed a units library. It is available on NuGet here . The repository is on my bitbucket account linked from my profile here on SO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM