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(); }
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(); }
public static double ConvertWeight(double measurement, SupportedWeight supported, ConvertToWeight convertTo) { double result = double.NaN; if (!double.IsNaN(measurement)) { switch (supported) { case SupportedWeight.Gram: switch (convertTo) { case ConvertToWeight.Grams: result = measurement; break; case ConvertToWeight.Millgrams: result = measurement * 1000; break; case ConvertToWeight.Kilograms: result = measurement * 1e-3; break; default: System.Diagnostics.Debug.WriteLine($"{supported} is not supported."); break; } break; case SupportedWeight.Kilogram: switch (convertTo) { case ConvertToWeight.Grams: result = measurement * 1000; break; case ConvertToWeight.Millgrams: result = measurement * 1e6; break; case ConvertToWeight.Kilograms: result = measurement; break; default: System.Diagnostics.Debug.WriteLine($"{supported} is not supported."); break; } break; case SupportedWeight.Ounce: switch (convertTo) { case ConvertToWeight.Grams: result = measurement * Constants.OunceToGram; break; case ConvertToWeight.Millgrams: result = measurement * Constants.OunceToGram * 1000; break; case ConvertToWeight.Kilograms: result = measurement * Constants.OunceToGram / 1000; break; default: System.Diagnostics.Debug.WriteLine($"{supported} is not supported."); break; } break; case SupportedWeight.Pound: switch (convertTo) { case ConvertToWeight.Grams: result = measurement * Constants.PoundToGram; break; case ConvertToWeight.Millgrams: result = measurement * Constants.PoundToGram * 1000; break; case ConvertToWeight.Kilograms: result = measurement * Constants.PoundToKg; break; default: System.Diagnostics.Debug.WriteLine($"{supported} is not supported."); break; } break; default: break; } return(result); } return(measurement); }