C# 食物量单位转换库
我想实现一个简单的食物测量单位转换库,即杯子茶匙。捏、毫升、盎司、升、克、磅等。 是否有任何我可以使用的库,如果没有,我想以下面的伪方式推出自己的库:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@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.
我最近在 GitHub 和 NuGet。
它为您提供所有常用单位和转换。它是轻量级的,经过单元测试并支持 PCL。
虽然我还没有合并所有的食物单位,但它已经在要添加的内容列表中了。
转换示例:
解析并获取特定于文化的缩写:
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:
Parse and Get Culture-Specific Abbreviations:
我不久前写过这个
http://www.carbonatethis.com/archives/38
I wrote this a while ago
http://www.carbonatethis.com/archives/38
不知道它是否能满足您的所有需求,但这看起来涵盖了很好的测量范围:
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
我还开发了一个单位库。它可以在 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.