TryParseDouble() public static method

public static TryParseDouble ( [ s, double &value ) : bool
s [
value double
return bool
Beispiel #1
0
        public GeoTagsEntry(string lat, string lng)
        {
            Latitude  = lat;
            Longitude = lng;

            LatitudeValue  = FlexibleParser.TryParseDouble(Latitude);
            LongitudeValue = FlexibleParser.TryParseDouble(Longitude);

            if (LatitudeValue.HasValue && Math.Abs(LatitudeValue.Value) > 90d)
            {
                LatitudeValue = null;
            }

            if (LongitudeValue.HasValue && Math.Abs(LongitudeValue.Value) > 180d)
            {
                LongitudeValue = null;
            }

            IsEmptyOrInvalid = !LatitudeValue.HasValue || !LongitudeValue.HasValue;
            if (IsEmptyOrInvalid)
            {
                return;
            }

            if (Latitude.ToLower().Contains("s"))
            {
                LatitudeValue = -Math.Abs(LatitudeValue ?? 0d);
            }

            if (Longitude.ToLower().Contains("w"))
            {
                LongitudeValue = -Math.Abs(LongitudeValue ?? 0d);
            }
        }
Beispiel #2
0
        public static bool DiapasonContains([NotNull] this string s, double value, bool roundSingle = true)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            return(s.Split(',', ';').Select(x => x.Trim()).Any(part => {
                var n = part.IndexOf('-');
                if (n == 0)
                {
                    var m = part.IndexOf('-', n + 1);
                    if (m != -1 && m != 1)
                    {
                        n = m;
                    }
                }

                double fromValue, toValue;
                if (n > 0 && n < part.Length - 1)   // "x-y"
                {
                    if (FlexibleParser.TryParseDouble(part.Substring(0, n), out fromValue) &&
                        FlexibleParser.TryParseDouble(part.Substring(n + 1), out toValue))
                    {
                        return value >= fromValue && value <= toValue;
                    }
                }
                else if (n < 0)     // "x"
                {
                    if (FlexibleParser.TryParseDouble(part, out fromValue))
                    {
                        return roundSingle ? value.RoughlyEquals(fromValue) : Equals(fromValue, value);
                    }
                }
                else if (part.Length == 1)     // "-"
                {
                    return true;
                }
                else if (n == part.Length - 1)     // "x-"
                {
                    if (FlexibleParser.TryParseDouble(part.Substring(0, n), out fromValue))
                    {
                        return value >= fromValue;
                    }
                }
                else     // "-x"
                {
                    if (FlexibleParser.TryParseDouble(part.Substring(n + 1), out toValue))
                    {
                        return value <= toValue;
                    }
                }

                return false;
            }));
        }
Beispiel #3
0
 protected override bool TryParse(string value, out double parsed)
 {
     return(FlexibleParser.TryParseDouble(value, out parsed));
 }
Beispiel #4
0
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     return(reader.TokenType == JsonToken.Boolean ? ((bool)reader.Value ? 1d : 0d) : FlexibleParser.TryParseDouble(reader.Value?.ToString()) ?? 0d);
 }
Beispiel #5
0
        private static double?ParseCoordinate(string s, bool latMode)
        {
            var m = Regex.Matches(s, @"-?\d+([\.,]\d+)?").OfType <Match>().Select(x => FlexibleParser.TryParseDouble(x.Value) ?? 0d).ToList();

            if (m.Count < 1)
            {
                return(null);
            }

            var degrees      = m.ElementAtOr(0, 0d);
            var minutes      = m.ElementAtOr(1, 0d);
            var seconds      = m.ElementAtOr(2, 0d);
            var milliseconds = m.ElementAtOr(3, 0d);
            var sign         = degrees >= 0 ? 1 : -1;

            degrees = Math.Abs(degrees);

            if (m.Count == 1)
            {
                if (degrees > 909090)
                {
                    milliseconds = degrees;
                    degrees      = 0;
                }
                else if (degrees > 9090)
                {
                    var newDegrees = Math.Floor(degrees / 10000);
                    minutes = Math.Floor((degrees - newDegrees * 10000) / 100);
                    seconds = Math.Floor(degrees - newDegrees * 10000 - minutes * 100);
                    degrees = newDegrees;
                }
                else if (degrees > 360)
                {
                    var newDegrees = Math.Floor(degrees / 100);
                    minutes = degrees - newDegrees * 100;
                    degrees = newDegrees;
                }
            }

            var result = sign * (degrees + minutes / 60 + seconds / 3600 + milliseconds / 3600000);

            if (Math.Abs(result) > (latMode ? 90d : 180d))
            {
                return(null);
            }

            if (s.ToLower().Contains(latMode ? "s" : "w"))
            {
                result = -result;
            }

            return(result);
        }