示例#1
0
        /// <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);
        }
示例#2
0
        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);
        }
示例#3
0
        /// <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);
        }
示例#4
0
        /// <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;
        }