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 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 void TestLengthConvert(string test, double expected, string expectedUnits) { if (ValueWithUnits.TryParse(test, out ValueWithUnits valueWithUnits)) { var converted = FilamentMath.ConvertLength(valueWithUnits.Value, FilamentMath.SupportedLengthAlias(valueWithUnits.Units), ConvertToLength.Millimeter); Assert.AreEqual(true, converted.WithinTolerance(expected, matchTolerance)); Assert.AreEqual(expectedUnits, valueWithUnits.Units); } else { Assert.Fail(); } }
public void TestConvertCompoundLength(string test, double expected, double tolerance, bool outcome) { if (CompoundFractionWithUnits.TryParse(test, out CompoundFractionWithUnits value)) { //int roundDigits = 4; var converted = FilamentMath.ConvertLength(value.Value, FilamentMath.SupportedLengthAlias(value.Units), ConvertToLength.Millimeter); //var rounded = Math.Round(converted, roundDigits); //var meetsTolerance = Math.Abs(converted - expected) < tolerance; Assert.AreEqual(outcome, converted.WithinTolerance(expected, tolerance)); } else { Assert.Fail(); } }
public object ConvertBack(object value, Type targetType, object parameter, string language) { bool unitsSet = false; ConvertToLength expectedUnits; //double conversionFactor = 1; if (parameter is string expected) { if (Enum.TryParse <ConvertToLength>(expected, out expectedUnits)) { unitsSet = true; } } else if (parameter is ConvertToLength actual) { expectedUnits = actual; unitsSet = true; } else { expectedUnits = ConvertToLength.Millimeter; unitsSet = true; } if (value is string str && unitsSet) { // TODO: implement a conversion factor for the units if (ValueWithUnits.TryParse(str, out ValueWithUnits valueWithUnits)) { return(FilamentMath.ConvertLength(valueWithUnits.Value, FilamentMath.SupportedLengthAlias(valueWithUnits.Units), expectedUnits)); } else if (CompoundFractionWithUnits.TryParse(str, out CompoundFractionWithUnits compoundFractionWithUnits)) { return(FilamentMath.ConvertLength(compoundFractionWithUnits.Value, FilamentMath.SupportedLengthAlias(compoundFractionWithUnits.Units), expectedUnits)); } } return(value); //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 object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { ConvertToLength convertTo = ConvertToLength.Millimeter; string defaultUnits; if (parameter is string paraString) { if (Enum.TryParse(paraString, out ConvertToLength result)) { convertTo = result; } defaultUnits = shortHandnames[(int)convertTo]; } else { defaultUnits = shortHandnames[(int)convertTo]; } if (value is string str) { if (ValueWithUnits.TryParse(str, out ValueWithUnits valueWithUnits)) { //var match = Regex.Match(str, WeightWithUnitsConverter.regexFindNumberAndUnit); SupportedLength supported = FilamentMath.SupportedLengthAlias(string.IsNullOrEmpty(valueWithUnits.Units) ? defaultUnits : valueWithUnits.Units); return(FilamentMath.ConvertLength(valueWithUnits.Value, supported, convertTo)); } else if (CompoundFractionWithUnits.TryParse(str, out CompoundFractionWithUnits compoundFractionWithUnits)) { //var fracMatch = Regex.Match(str, regexFindFraction); SupportedLength supported = FilamentMath.SupportedLengthAlias(!string.IsNullOrEmpty(compoundFractionWithUnits.Units) ? compoundFractionWithUnits.Units : "in"); return(FilamentMath.ConvertLength(compoundFractionWithUnits.Value, supported, convertTo)); //double number; //double numerator; //double denominator; //if (double.TryParse(fracMatch.Groups["numerator"].Value, out numerator) && double.TryParse(fracMatch.Groups["denominator"].Value, out denominator)) //{ // if (!string.IsNullOrEmpty(fracMatch.Groups["whole"].Value)) // { // if (double.TryParse(fracMatch.Groups["whole"].Value, out number)) // { // return (number + (numerator / denominator)) * ConversionFactor(string.IsNullOrEmpty(fracMatch.Groups["units"].Value) ? "in" : fracMatch.Groups["units"].Value.ToLower(), convertTo); // } // else // return double.NaN; // } // else // { // return (numerator / denominator) * ConversionFactor(string.IsNullOrEmpty(fracMatch.Groups["units"].Value) ? "in" : fracMatch.Groups["units"].Value.ToLower(), convertTo); // } } else { return(double.NaN); } } else { return(double.NaN); } }