private string GetResult(double value, Unit unit, Unit toUnit)
        {
            var sb = new StringBuilder();

            foreach (var conversion in Units.Conversions)
            {
                if (conversion.From == unit && conversion.To == toUnit)
                {
                    var result = GetResult(value, conversion);
                    sb.Append(result);
                }
            }

            if (sb.Length == 0)
            {
                Conversion first = null;
                Conversion second = null;

                // no direct conversion, try 2-step chain
                foreach (var conversion in Units.Conversions)
                {
                    if (conversion.From == unit)
                    {
                        first = conversion;
                    }

                    if (conversion.To == toUnit)
                    {
                        second = conversion;
                    }
                }

                if (first != null && second != null && first.To == second.From)
                {
                    var composite = new Conversion(first.From, second.To, v => second.Converter(first.Converter(v)));
                    sb.Append(GetResult(value, composite));
                }
            }

            return sb.ToString();
        }
        private string GetResult(double value, Unit unit)
        {
            var sb = new StringBuilder();

            foreach (var conversion in Units.Conversions)
            {
                if (conversion.From == unit)
                {
                    var result = GetResult(value, conversion);
                    sb.Append(result);
                }
            }

            if (sb.Length == 0)
            {
                return null;
            }

            return sb.ToString();
        }
Пример #3
0
 public Conversion(Unit from, Unit to, Func<double, double> converter)
 {
     From = from;
     To = to;
     Converter = converter;
 }