.NET 单位等级,英寸到毫米
.NET 有单位转换类吗?我需要将英寸转换为毫米,反之亦然。
Does .NET has Units conversion class? I need to convert inches to millimeters and vise versa.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,没有类似的内置功能。但您可以简单地乘或除以 25.4。
No there is nothing like that built-in. But you can simply multiply or divide by 25.4.
我以前处理过这个问题。我建议针对距离开设两门课程。一种具有英制尺寸,一种具有公制尺寸。然后,您可以轻松地在它们之间来回转换,但需要注意的是,这样做时会失去精度。
以下是英制距离等级的示例,以英寸为基本测量单位。
这是一个以米为基本单位的度量类:
这是一个示例用法的测试方法。
您还可以添加扩展方法:
您可以使用这个简单的策略来测量重量、温度、液体测量等。
I've dealt with this issue before. I recommend making two classes for distance. One that has the imperial measures and one that has the metric measures. Then, you can convert back and forth between them easily, with the obvious caveat that you lose precision when you do.
Here's an example of an imperial distance class, with inches as the base unit of measure.
And here's a metric class with meters as the base unit:
And here's a test method that exemplifies the usage.
You can also add extension methods:
You can use this simple strategy for weights, temperatures, liquid measures, etc.
.NET Framework 没有这样的东西,但 F# 有 测量单位。
.NET Framework doesn't have anything like this, but F# does Units of Measure.
不,框架中没有内置此类单位转换。不过,您自己实施应该很容易。
No, there is no such unit conversions built into the framework. Should be easy enough to implement yourself, though.
Csunits 是一个很好的 C# 测量单位库,请参阅 https://github.com/cureos/csunits 。它目前适用于放射治疗,但您可以轻松添加自己的单位和数量。
Csunits is a nice Units of Measurement library for C#, see https://github.com/cureos/csunits. It's currently geared towards radiotherapy but you can easily add your own units and quantities.
不,你需要自己制作一个,如下所示:
No, you need to make one yourself, like this:
几年后,有很多 Nuget 包可以处理这个问题。其中之一是 https://github.com/angularsen/UnitsNet 它能够处理您想要的内容想。
它还支持很多其他东西。
A few Years later there are plenty of Nuget Packages that deal with this. One of them is https://github.com/angularsen/UnitsNet which is able to handle what you want.
It also supports a lot of other stuff.