Класс единиц .NET, дюймы в миллиметры - PullRequest
12 голосов
/ 17 мая 2011

Есть ли в .NET класс преобразования единиц? Мне нужно конвертировать дюймы в миллиметры и наоборот.

Ответы [ 7 ]

11 голосов
/ 08 ноября 2012

Я имел дело с этой проблемой раньше.Я рекомендую сделать два занятия на расстоянии.Тот, который имеет имперские меры, и тот, который имеет метрические меры.Затем вы можете легко конвертировать их назад и вперед, с очевидным предостережением о том, что при этом вы теряете точность.

Вот пример класса имперского расстояния с базовой единицей измерения в дюймах.

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

А вот метод тестирования, который иллюстрирует использование.добавить методы расширения

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

Вы можете использовать эту простую стратегию для весов, температур, показателей жидкости и т. д.

11 голосов
/ 17 мая 2011

Нет, ничего такого встроенного нет. Но вы можете просто умножить или разделить на 25,4.

3 голосов
/ 17 мая 2011

.NET Framework не имеет ничего подобного, но F # имеет Единицы измерения .

2 голосов
/ 17 мая 2011

Csunits - хорошая библиотека единиц измерения для C #, см. https://github.com/cureos/csunits. В настоящее время она ориентирована на лучевую терапию, но вы можете легко добавлять свои собственные единицы измерения и количества.

2 голосов
/ 17 мая 2011

Нет, вам нужно сделать его самостоятельно, например:

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; } }
}
2 голосов
/ 17 мая 2011

Нет, нет таких преобразований, встроенных в каркас. Хотя должно быть достаточно легко реализовать себя.

1 голос
/ 24 апреля 2018

Через несколько лет появилось множество пакетов Nuget, которые занимаются этим. Одним из них является https://github.com/angularsen/UnitsNet, который способен справиться с тем, что вы хотите.

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

Он также поддерживает много других вещей.

...