.NET 单位等级,英寸到毫米

发布于 2024-11-07 20:43:46 字数 37 浏览 3 评论 0原文

.NET 有单位转换类吗?我需要将英寸转换为毫米,反之亦然。

Does .NET has Units conversion class? I need to convert inches to millimeters and vise versa.

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

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

发布评论

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

评论(7

温柔少女心 2024-11-14 20:43:46

不,没有类似的内置功能。但您可以简单地乘或除以 25.4。

No there is nothing like that built-in. But you can simply multiply or divide by 25.4.

滴情不沾 2024-11-14 20:43:46

我以前处理过这个问题。我建议针对距离开设两门课程。一种具有英制尺寸,一种具有公制尺寸。然后,您可以轻松地在它们之间来回转换,但需要注意的是,这样做时会失去精度。

以下是英制距离等级的示例,以英寸为基本测量单位。

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());
}

您可以使用这个简单的策略来测量重量、温度、液体测量等。

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.

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);
    }
}

And here's a metric class with meters as the base unit:

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);
    }
}

And here's a test method that exemplifies the usage.

[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);
}

You can also add extension methods:

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());
}

You can use this simple strategy for weights, temperatures, liquid measures, etc.

不羁少年 2024-11-14 20:43:46

.NET Framework 没有这样的东西,但 F# 有 测量单位

.NET Framework doesn't have anything like this, but F# does Units of Measure.

伪心 2024-11-14 20:43:46

不,框架中没有内置此类单位转换。不过,您自己实施应该很容易。

No, there is no such unit conversions built into the framework. Should be easy enough to implement yourself, though.

寄与心 2024-11-14 20:43:46

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.

浮生面具三千个 2024-11-14 20:43:46

不,你需要自己制作一个,如下所示:

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; } }
}

No, you need to make one yourself, like this:

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; } }
}
不必了 2024-11-14 20:43:46

几年后,有很多 Nuget 包可以处理这个问题。其中之一是 https://github.com/angularsen/UnitsNet 它能够处理您想要的内容想。

Length distance = Length.FromInch(15);
distance.Millimeters/ 60;

它还支持很多其他东西。

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.

Length distance = Length.FromInch(15);
distance.Millimeters/ 60;

It also supports a lot of other stuff.

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