C# 食物量单位转换库

发布于 2024-10-26 01:27:56 字数 1014 浏览 3 评论 0原文

我想实现一个简单的食物测量单位转换库,即杯子茶匙。捏、毫升、盎司、升、克、磅等。 是否有任何我可以使用的库,如果没有,我想以下面的伪方式推出自己的库:

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

指针,陷阱,太天真了?任何帮助都会有用的。 我可以研究的当前开源实现也很棒。 TIA

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沙沙粒小 2024-11-02 01:27:56

@n4rzul,我已经开始用 C# 开发一个开源测量单位库,可以轻松地根据您请求的任何单位进行扩展。请查看 Github 上的项目网站,https://github.com/cureos/csunits,看看这个库是否满足您的需求。

@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.

枕头说它不想醒 2024-11-02 01:27:56

我最近在 GitHubNuGet

它为您提供所有常用单位和转换。它是轻量级的,经过单元测试并支持 PCL。

虽然我还没有合并所有的食物单位,但它已经在要添加的内容列表中了。

转换示例:

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

解析并获取特定于文化的缩写:

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)

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)
唐婉 2024-11-02 01:27:56

不知道它是否能满足您的所有需求,但这看起来涵盖了很好的测量范围:

http://unitconvertlibrary。 codeplex.com/

虽然它看起来正在开发中,并且源存储库中只有一个类可用

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

浊酒尽余欢 2024-11-02 01:27:56

我还开发了一个单位库。它可以在 NuGet 此处 上找到。该存储库位于我的 bitbucket 帐户上,该帐户是从我的个人资料链接到的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文