/// <summary> /// Given parsed ConvertModel, computes result. (E.g "1 foot in cm"). /// </summary> /// <returns>The converted value as a double.</returns> public static double ConvertInput(ConvertModel convertModel, QuantityType quantityType) { QuantityInfo unitInfo = Quantity.GetInfo(quantityType); var fromUnit = GetUnitEnum(convertModel.FromUnit, unitInfo); var toUnit = GetUnitEnum(convertModel.ToUnit, unitInfo); if (fromUnit != null && toUnit != null) { return(UnitsNet.UnitConverter.Convert(convertModel.Value, fromUnit, toUnit)); } return(double.NaN); }
/// <summary> /// Given ConvertModel returns collection of possible results. /// </summary> /// <returns>The converted value as a double.</returns> public static IEnumerable <UnitConversionResult> Convert(ConvertModel convertModel) { var results = new List <UnitConversionResult>(); foreach (QuantityType quantityType in _included) { double convertedValue = UnitHandler.ConvertInput(convertModel, quantityType); if (!double.IsNaN(convertedValue)) { UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityType); results.Add(result); } } return(results); }
public List <Result> Query(Query query) { if (query == null) { throw new ArgumentNullException(paramName: nameof(query)); } // Parse ConvertModel convertModel = InputInterpreter.Parse(query); if (convertModel == null) { return(new List <Result>()); } // Convert return(UnitHandler.Convert(convertModel) .Select(x => GetResult(x)) .ToList()); }