事业单位c类网课:.NET单位类 英寸到毫米(inches to ml conversion)

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

14

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

14

我以前处理过这个问题。我建议为距离制作两个类。一个具有英制度量,另一个具有公制度量。然后,您可以轻松地在它们之间来回转换,明显的警告是,当您这样做时,您会失去精度。

这里有一个英制距离类的例子,以英寸为基本度量单位。

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

您可以将此简单策略用于重量,温度,液体测量等。

3

.NET Framework 没有这样的功能,但 F # 有Units of Measure

2

不,框架中没有这样的单位转换。不过,应该很容易实现自己。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(345)
脚本语言:脚本语言与编程语言(scripting languages list)
上一篇
单位机构代码是什么:Xcode中的计量单位是什么(how to use measurement on iphone)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(7条)