Beispiel #1
0
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            ConvertToWeight expected = ConvertToWeight.Kilograms;

            if (parameter is ConvertToWeight supportedWeight)
            {
                expected = supportedWeight;
            }
            else if (parameter is string str)
            {
                if (Enum.TryParse <ConvertToWeight>(str, out ConvertToWeight supported))
                {
                    expected = supported;
                }
            }

            if (value is string content)
            {
                if (ValueWithUnits.TryParse(content, out ValueWithUnits valueWithUnits))
                {
                    return(FilamentMath.ConvertWeight(valueWithUnits.Value, FilamentMath.SupportedWeightAlias(valueWithUnits.Units), expected));
                }
            }

            return(double.NaN);
            //throw new NotImplementedException();
        }
Beispiel #2
0
 public void TestWeightConvert(string test, double expected, string expectedUnits)
 {
     if (ValueWithUnits.TryParse(test, out ValueWithUnits valueWithUnits))
     {
         var converted = FilamentMath.ConvertWeight(valueWithUnits.Value, FilamentMath.SupportedWeightAlias(valueWithUnits.Units.ToLower()), ConvertToWeight.Kilograms);
         Assert.AreEqual(true, converted.WithinTolerance(expected, matchTolerance));
         Assert.AreEqual(expectedUnits.ToLower(), valueWithUnits.Units.ToLower());
     }
 }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ConvertToWeight convertTo = ConvertToWeight.Kilograms;
            string          defaultUnits;

            if (parameter is string paraString)
            {
                if (Enum.TryParse(paraString, out ConvertToWeight result))
                {
                    convertTo    = result;
                    defaultUnits = shortHandnames[(int)result];
                }
                else
                {
                    defaultUnits = shortHandnames[(int)convertTo];
                }
            }
            else
            {
                defaultUnits = shortHandnames[(int)convertTo];
            }
            if (value is string str)
            {
                //if (str.Contains(space))
                //{
                //    var args = str.Split(space);
                //    if (args.Length ==2 && args.Count(a => a.Length > 0) == 2)
                //    {
                //        if (double.TryParse(args[0], out double result))
                //            return result * ConversionFactor(args[1].ToLower(), convertTo);
                //        else
                //            return double.NaN;
                //    }
                //}
                //else if (str.Count(ch => char.IsLetter(ch)) > 0)
                //{
                //    var num = new string(str.Where(ch=>char.IsDigit(ch)||ch== '.').ToArray());
                //    var units = new string(str.Where(ch => char.IsLetter(ch)).ToArray());
                //    if(double.TryParse(num, out double result))
                //        return result * ConversionFactor(units.ToLower(),convertTo);
                //}
                //if (Regex.IsMatch(str, regexFindNumberAndUnit))
                //{
                //    var match = Regex.Match(str, regexFindNumberAndUnit);
                //    if (double.TryParse(match.Groups["number"].Value, out double result))
                //        return result * ConversionFactor(String.IsNullOrEmpty(match.Groups["units"].Value) ? defaultUnits : match.Groups["units"].Value, convertTo);
                //    else
                //        return double.NaN;
                //}
                if (ValueWithUnits.TryParse(str, out ValueWithUnits valueWithUnits))
                {
                    return(FilamentMath.ConvertWeight(valueWithUnits.Value,
                                                      FilamentMath.SupportedWeightAlias(string.IsNullOrEmpty(valueWithUnits.Units) ? defaultUnits : valueWithUnits.Units),
                                                      convertTo));
                }
                else
                {
                    return(double.NaN);
                }
            }
            if (value is double d)
            {
                return(d);
            }
            else
            {
                return(double.NaN);
            }

            //throw new NotImplementedException();
        }