using LoreSoft.MathExpressions.Metadata; namespace LoreSoft.MathExpressions.UnitConversion { /// Units for Temperature public enum TemperatureUnit { /// Degrees Celsius unit (c) [Abbreviation("c")] Celsius = 0, /// Degrees Fahrenheit unit (f) [Abbreviation("f")] Fahrenheit = 1, /// Degrees Kelvin unit (k) [Abbreviation("k")] Kelvin = 2 } /// /// Class representing temperature convertion. /// public static class TemperatureConverter { /// /// 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( TemperatureUnit fromUnit, TemperatureUnit toUnit, double fromValue) { if (fromUnit == toUnit) return fromValue; double result = 0; if (fromUnit == TemperatureUnit.Celsius) { if (toUnit == TemperatureUnit.Kelvin) result = fromValue + 273.15d; else if (toUnit == TemperatureUnit.Fahrenheit) //(9/5 * C) + 32 = F result = (9.0d/5.0d*fromValue) + 32d; } else if (fromUnit == TemperatureUnit.Kelvin) { if (toUnit == TemperatureUnit.Celsius) result = fromValue - 273.15d; else if (toUnit == TemperatureUnit.Fahrenheit) result = 5.0d/9.0d*((fromValue - 273.15d) + 32d); } else if (fromUnit == TemperatureUnit.Fahrenheit) { if (toUnit == TemperatureUnit.Celsius) //(F - 32) * 5/9 = C result = 5.0d/9.0d*(fromValue - 32d); else if (toUnit == TemperatureUnit.Kelvin) result = (5.0d/9.0d*(fromValue - 32d)) + 273.15; } return result; } } }