/// <summary> /// Tries to parse a string representing a speed. Assumes kilometers per hour by default, use explicit parsing methods for different behaviour. /// </summary> /// <param name="s"></param> /// <param name="result"></param> /// <returns></returns> public static bool TryParse(string s, out Speed result) { result = null; if (s.IsNullOrWhiteSpace()) { return(false); } // try a generic parse first, in this case assume kilometers per hour. double value; if (double.TryParse(s, out value)) { // the value is just a numeric value. result = new KilometerPerHour(value); return(true); } // try kilometers per hour. if (KilometerPerHour.TryParse(s, out result)) { // succes! return(true); } // try miles per hour. if (MilesPerHour.TryParse(s, out result)) { // success! return(true); } // try knots. if (Knots.TryParse(s, out result)) { // success! return(true); } // try meters per second. if (MeterPerSecond.TryParse(s, out result)) { // success! return(true); } return(false); }
public static bool TryParse(string s, out MeterPerSecond result) { result = (MeterPerSecond)null; double result1; if (double.TryParse(s, NumberStyles.Any, (IFormatProvider)CultureInfo.InvariantCulture, out result1)) { result = new MeterPerSecond(result1); return(true); } Match match = new Regex("^\\s*(\\d+(?:\\.\\d*)?)\\s*\\s*(m/s)?\\s*$", RegexOptions.IgnoreCase).Match(s); if (!match.Success) { return(false); } result = new MeterPerSecond(double.Parse(match.Groups[1].Value, (IFormatProvider)CultureInfo.InvariantCulture)); return(true); }
/// <summary> /// Tries to parse a string containing a meters per second value. /// </summary> /// <param name="s"></param> /// <param name="result"></param> /// <returns></returns> public static bool TryParse(string s, out MeterPerSecond result) { result = null; double value; if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) { // the value is just a numeric value. result = new MeterPerSecond(value); return(true); } // do some more parsing work. Regex regex = new Regex("^" + Constants.RegexDecimalWhiteSpace + MeterPerSecond.RegexUnitMetersPerSecond + "$", RegexOptions.IgnoreCase); Match match = regex.Match(s); if (match.Success) { result = new MeterPerSecond(double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)); return(true); } return(false); }
/// <summary> /// Tries to parse a string containing a meters per second value. /// </summary> /// <param name="s"></param> /// <param name="result"></param> /// <returns></returns> public static bool TryParse(string s, out MeterPerSecond result) { result = null; double value; if (double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) { // the value is just a numeric value. result = new MeterPerSecond(value); return true; } // do some more parsing work. Regex regex = new Regex("^" + Constants.RegexDecimalWhiteSpace + MeterPerSecond.RegexUnitMetersPerSecond + "$", RegexOptions.IgnoreCase); Match match = regex.Match(s); if (match.Success) { result = new MeterPerSecond(double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)); return true; } return false; }