Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        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());
        }