.NET是否具有单位转换类?我需要将英寸转换为毫米,反之亦然。.NET单位类,英寸到毫米
回答
不,没有像内置的东西。但你可以简单地乘以或除以25.4。
不,没有这样的单位转换内置到框架。虽然应该很容易实现自己。
不,你需要自己做一个,这样的:
public class Length
{
private const double MillimetersPerInch = 25.4;
private double _Millimeters;
public static Length FromMillimeters(double mm)
{
return new Length { _Millimeters = mm };
}
public static Length FromInch(double inch)
{
return new Length { _Millimeters = inch * MillimetersPerInch };
}
public double Inch { get { return _Millimeters/MillimetersPerInch; } }
public double Millimeters { get { return _Millimeters; } }
}
.NET框架没有这样的事,但F#不Units of Measure。
Csunits是C#的一个很好的测量单位库,见https://github.com/cureos/csunits。它目前适用于放射治疗,但您可以轻松添加自己的单位和数量。
我以前处理过这个问题。我建议为距离做两个类。一种具有帝国措施,另一种具有度量措施。然后,您可以轻松地在它们之间来回转换,但有一个明显的警告,那就是您在执行时失去了精度。
下面是以英寸为度量单位的英制距离等级的示例。
public class ImperialDistance {
public static readonly ImperialDistance Inch = new ImperialDistance(1.0);
public static readonly ImperialDistance Foot = new ImperialDistance(12.0);
public static readonly ImperialDistance Yard = new ImperialDistance(36.0);
public static readonly ImperialDistance Mile = new ImperialDistance(63360.0);
private double _inches;
public ImperialDistance(double inches) {
_inches = inches;
}
public double ToInches() {
return _inches;
}
public double ToFeet() {
return _inches/Foot._inches;
}
public double ToYards() {
return _inches/Yard._inches;
}
public double ToMiles() {
return _inches/Mile._inches;
}
public MetricDistance ToMetricDistance() {
return new MetricDistance(_inches * 0.0254);
}
public override int GetHashCode() {
return _inches.GetHashCode();
}
public override bool Equals(object obj) {
var o = obj as ImperialDistance;
if (o == null) return false;
return _inches.Equals(o._inches);
}
public static bool operator ==(ImperialDistance a, ImperialDistance b) {
// If both are null, or both are same instance, return true
if (ReferenceEquals(a, b)) return true;
// if either one or the other are null, return false
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false;
// compare
return a._inches == b._inches;
}
public static bool operator !=(ImperialDistance a, ImperialDistance b) {
return !(a == b);
}
public static ImperialDistance operator +(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches + b._inches);
}
public static ImperialDistance operator -(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches - b._inches);
}
public static ImperialDistance operator *(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches * b._inches);
}
public static ImperialDistance operator /(ImperialDistance a, ImperialDistance b) {
if (a == null) throw new ArgumentNullException();
if (b == null) throw new ArgumentNullException();
return new ImperialDistance(a._inches/b._inches);
}
}
而这里的度量类如米,基本单元:
public class MetricDistance {
public static readonly MetricDistance Milimeter = new MetricDistance(0.001);
public static readonly MetricDistance Centimeter = new MetricDistance(0.01);
public static readonly MetricDistance Decimeter = new MetricDistance(0.1);
public static readonly MetricDistance Meter = new MetricDistance(1.0);
public static readonly MetricDistance Decameter = new MetricDistance(10.0);
public static readonly MetricDistance Hectometer = new MetricDistance(100.0);
public static readonly MetricDistance Kilometer = new MetricDistance(1000.0);
private double _meters;
public MetricDistance(double meters) {
_meters = meters;
}
public double ToMilimeters() {
return _meters/Milimeter._meters;
}
public double ToCentimeters() {
return _meters/Centimeter._meters;
}
public double ToDecimeters() {
return _meters/Decimeter._meters;
}
public double ToMeters() {
return _meters;
}
public double ToDecameters() {
return _meters/Decameter._meters;
}
public double ToHectometers() {
return _meters/Hectometer._meters;
}
public double ToKilometers() {
return _meters/Kilometer._meters;
}
public ImperialDistance ToImperialDistance() {
return new ImperialDistance(_meters * 39.3701);
}
public override int GetHashCode() {
return _meters.GetHashCode();
}
public override bool Equals(object obj) {
var o = obj as MetricDistance;
if (o == null) return false;
return _meters.Equals(o._meters);
}
public static bool operator ==(MetricDistance a, MetricDistance b) {
// If both are null, or both are same instance, return true
if (ReferenceEquals(a, b)) return true;
// if either one or the other are null, return false
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false;
return a._meters == b._meters;
}
public static bool operator !=(MetricDistance a, MetricDistance b) {
return !(a == b);
}
public static MetricDistance operator +(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters + b._meters);
}
public static MetricDistance operator -(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters - b._meters);
}
public static MetricDistance operator *(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters * b._meters);
}
public static MetricDistance operator /(MetricDistance a, MetricDistance b) {
if (a == null) throw new ArgumentNullException("a");
if (b == null) throw new ArgumentNullException("b");
return new MetricDistance(a._meters/b._meters);
}
}
及这里的例证使用的测试方法。
[TestMethod]
public void _5in_Equals_12_7cm() {
var inches = new ImperialDistance(5);
var cms = new MetricDistance(MetricDistance.Centimeter.ToMeters() * 12.7);
var calcCentimeters = Math.Round(inches.ToMetricDistance().ToCentimeters(), 2, MidpointRounding.AwayFromZero);
var calcInches = Math.Round(cms.ToImperialDistance().ToInches(), 2, MidpointRounding.AwayFromZero);
Assert.AreEqual(cms.ToCentimeters(), 12.7);
Assert.AreEqual(calcCentimeters, 12.7);
Assert.AreEqual(inches.ToInches(), 5);
Assert.AreEqual(calcInches, 5);
}
您还可以添加扩展方法
public static MetricDistance Centimeters(this Int32 that) {
return new MetricDistance(MetricDistance.Centimeter.ToMeters() * that);
}
[TestMethod]
public void _100cm_plus_300cm_equals_400cm() {
Assert.AreEqual(100.Centimeters() + 300.Centimeters(), 400.Centimeters());
}
您可以使用重量,温度,液体措施,这个简单的策略,等
'MetricDistance'和'ImperialDistance'都应该是结构。 – Georg 2017-06-14 06:50:59
这是一个选项,当然。 – mattmc3 2017-06-14 13:56:07
[UnitsNet](https://nuget.org/packages/UnitsNet)nuget采取了类似的方法,但涵盖了大量的数量和单位。 – angularsen 2017-09-30 17:11:42
- 1. 的Excel英尺和英寸到毫米
- 2. CSS英寸/毫米测量不准确
- 3. 如何使用单位像英寸和毫米工作织物JS
- 4. 厘米到英尺和英寸转换
- 5. Android:尺寸为毫米或英寸的视图尺寸不正确
- 6. 以厘米(英寸)为单位的Div宽度
- 7. 使用物理单位的Android Webview(厘米/英寸)
- 8. .net图表控件 - 以毫米为单位显示值:ss
- 9. 如何在matplotlib中以毫米为单位设置轴的尺寸和位置?
- 10. 米到英尺
- 11. TCPDF创建标签PDF尺寸宽度:57毫米和高度:32毫米
- 12. JAVA - 显示英制转换为米。每12英寸输出一个空行。 (一米等于大约39.37英寸)
- 13. 将厘米转换为英寸
- 14. 我需要将QGraphicsScene打印为实际(英寸/毫米)的刻度
- 15. iOS按钮位置x,y 3,5英寸到4英寸
- 16. 如何将报表设计器标尺单位从英寸更改为厘米?
- 17. PrintDocument_PrintPage&Graphics.DrawImage大小以英寸为单位
- 18. .NET打印 - 从毫米到像素的转换
- 19. 定义以毫米为单位的绘图元素大小(R)
- 20. 在面板上以毫米为单位对齐网格?
- 21. iOS设备实际尺寸以英寸为单位
- 22. 转换12小时hh:毫米上午/下午到24小时hh:毫米
- 23. Inkscape:如何使用以毫米为单位的移位创建平铺克隆
- 24. 我在转换英寸到厘米的转换表格时遇到问题
- 25. 厘米转换为英尺和英寸,反之亦然不同的输出?
- 26. 单独的“英尺/英寸”列到两列
- 27. Graphics.PageUnit毫米的C#打印
- 28. 将毫米转换为em
- 29. Android的 - 以毫米为
- 30. 使用Jframe将英寸转换为厘米
出了什么问题'*'和'/'? – Bobby 2011-05-17 07:37:27
没有错,但如果有这样的类,为什么我应该再次发明车轮;) – Tomas 2011-05-17 07:39:01