using LoreSoft.MathExpressions.Metadata; using System.ComponentModel; namespace LoreSoft.MathExpressions.UnitConversion { /// Units for Speed public enum SpeedUnit { /// Meter/Second unit (m/s) [Abbreviation("m/s")] [Description("Meter/Second")] MeterPerSecond = 0, /// Kilometer/Hour unit (kph) [Abbreviation("kph")] [Description("Kilometer/Hour")] KilometerPerHour = 1, /// Foot/Second unit (ft/s) [Abbreviation("ft/s")] [Description("Foot/Second")] FootPerSecond = 2, /// Mile/Hour unit (mph) [Abbreviation("mph")] [Description("Mile/Hour")] MilePerHour = 3, /// Knot unit (knot) [Abbreviation("knot")] Knot = 4, /// Mach unit (mach) [Abbreviation("mach")] Mach = 5, } /// /// Class representing speed convertion. /// public static class SpeedConverter { // In enum order private static readonly double[] factors = new double[] { 1d, //meter/second 1000d/3600d, //kilometer/hour 0.3048d, //foot/second (0.3048d*5280d)/3600d, //mile/hour (mph) 1852d/3600d, //knot 340.29d, //mach }; /// /// Converts the specified from unit to the specified unit. /// /// Covert from unit. /// Covert to unit. /// Covert from value. /// The converted value. public static double Convert( SpeedUnit fromUnit, SpeedUnit toUnit, double fromValue) { if (fromUnit == toUnit) return fromValue; double fromFactor = factors[(int)fromUnit]; double toFactor = factors[(int)toUnit]; double result = fromFactor * fromValue / toFactor; return result; } } }