示例#1
0
        public static bool TryParse(string s, IFormatProvider provider, out Measure value, UnitOfMeasure defaultUnit = null)
        {
            value = Measure.Empty;
            //var numberFormat = NumberFormatInfo.GetInstance(provider);

            if (NumberHelper.SmartTryParse(s, provider, out double decimalValue))
            {
                value = new Measure(decimalValue, null);
            }
            else if (SimpleMeasurePattern.IsMatch(s))
            {
                var matchResult = SimpleMeasurePattern.Match(s);
                var numberStr   = matchResult.Groups[1].Value;
                var unit        = defaultUnit;

                if (matchResult.Groups[2].Success)
                {
                    unit = UnitOfMeasure.GetUnitByName(matchResult.Groups[2].Value.Trim());
                    if (unit == null)
                    {
                        return(false);
                    }
                }

                if (NumberHelper.SmartTryParse(numberStr, provider, out double measureValue))
                {
                    value = new Measure(measureValue, unit);
                }
            }
            else if (InchFractionPattern.IsMatch(s))
            {
                var           matchResult = InchFractionPattern.Match(s);
                var           whole       = matchResult.Groups[1].Success ? int.Parse(matchResult.Groups[1].Value) : 0;
                var           n1          = double.Parse(matchResult.Groups[2].Value);
                var           n2          = double.Parse(matchResult.Groups[3].Value);
                UnitOfMeasure unit        = defaultUnit ?? UnitOfMeasure.In;
                if (matchResult.Groups[4].Success)
                {
                    unit = UnitOfMeasure.GetUnitByName(matchResult.Groups[4].Value.Trim());
                    if (unit == null)
                    {
                        return(false);
                    }
                }
                value = new Measure(whole + (n1 / n2), unit);
            }

            if (!value.IsEmpty && value.Unit == null && defaultUnit != null)
            {
                value = new Measure(value.Value, defaultUnit);
            }

            return(!value.IsEmpty);
        }
示例#2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (objectType == typeof(UnitOfMeasure))
            {
                string unitAbv = (string)reader.Value;
                return(UnitOfMeasure.GetUnitByName(unitAbv));
            }
            else if (objectType == typeof(Measure))
            {
                return(Measure.ParseInvariant((string)reader.Value));
            }

            throw new NotImplementedException();
        }
示例#3
0
        public static Measure Parse(string s, IFormatProvider provider, UnitOfMeasure defaultUnit = null)
        {
            if (NumberHelper.SmartTryParse(s, provider, out double decimalValue))
            {
                return(new Measure(decimalValue, defaultUnit));
            }

            if (SimpleMeasurePattern.IsMatch(s))
            {
                var match     = SimpleMeasurePattern.Match(s);
                var numberStr = match.Groups[1].Value;
                var unit      = defaultUnit;

                if (match.Groups[2].Success)
                {
                    var unitName = match.Groups[2].Value.Trim();
                    unit = UnitOfMeasure.GetUnitByName(unitName);
                    if (unit == null)
                    {
                        throw new ArgumentException("#1 Invalid Unit of measure" + unitName);
                    }
                }

                if (NumberHelper.SmartTryParse(numberStr, provider, out double measureValue))
                {
                    return(new Measure(measureValue, unit));
                }
                else
                {
                    throw new ArgumentException("Invalid measure");
                }
            }
            else if (InchFractionPattern.IsMatch(s))
            {
                var           match = InchFractionPattern.Match(s);
                var           whole = match.Groups[1].Success ? int.Parse(match.Groups[1].Value) : 0;
                var           n1    = double.Parse(match.Groups[2].Value);
                var           n2    = double.Parse(match.Groups[3].Value);
                UnitOfMeasure unit  = defaultUnit ?? UnitOfMeasure.In;
                if (match.Groups[4].Success)
                {
                    unit = UnitOfMeasure.GetUnitByName(match.Groups[4].Value.Trim());
                    if (unit == null)
                    {
                        throw new ArgumentException("#2 Invalid Unit of measure" + match.Groups[4].Value);
                    }
                }

                return(new Measure(whole + (n1 / n2), unit));
            }
            else if (FeetFractionPattern.IsMatch(s))
            {
                var match     = FeetFractionPattern.Match(s);
                var wholeFeet = int.Parse(match.Groups[1].Value);
                var wholeInch = match.Groups[3].Success ? int.Parse(match.Groups[3].Value) : 0;
                var n1        = double.Parse(match.Groups[4].Value);
                var n2        = double.Parse(match.Groups[5].Value);

                return(new Measure(wholeFeet, UnitOfMeasure.Ft) + Measure.Inches(wholeInch + (n1 / n2)));
            }
            else if (s.ToUpper().Trim() == "N/A")
            {
                return(Measure.Empty);
            }

            throw new InvalidOperationException();
        }
示例#4
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                var measureStr = (string)value;
                if (!string.IsNullOrWhiteSpace(measureStr))
                {
                    if (ParseSimpleReg.IsMatch(measureStr))
                    {
                        var match     = ParseSimpleReg.Match(measureStr);
                        var numberStr = match.Groups[1].Value;
                        var unitName  = match.Groups[2].Value;
                        var unit      = UnitOfMeasure.GetUnitByName(unitName);
                        if (unit == null)
                        {
                            throw new ArgumentException("#1 Invalid Unit of measure" + unitName);
                        }

                        if (numberStr.Contains(".") || numberStr.Contains(","))
                        {
                            var cleanStr = (Func <string, string>)((str) =>
                            {
                                return(str.Replace(",", string.Empty).Replace(".", string.Empty).Replace(" ", string.Empty));
                            });

                            int idx   = numberStr.LastIndexOfAny(new char[] { '.', ',' });
                            var left  = cleanStr(numberStr.Substring(0, idx));
                            var right = cleanStr(numberStr.Substring(idx));
                            numberStr = left + NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + right;
                        }
                        double measureValue = 0;
                        if (double.TryParse(numberStr, out measureValue))
                        {
                            return(new Measure(measureValue, unit));
                        }
                        else
                        {
                            throw new ArgumentException("Invalid measure");
                        }
                    }
                    else if (ParseFractionReg.IsMatch(measureStr))
                    {
                        var match = ParseFractionReg.Match(measureStr);
                        var whole = match.Groups[1].Success ? int.Parse(match.Groups[1].Value) : 0;
                        var n1    = double.Parse(match.Groups[2].Value);
                        var n2    = double.Parse(match.Groups[3].Value);
                        var unit  = UnitOfMeasure.GetUnitByName(match.Groups[4].Value.Trim());
                        if (unit == null)
                        {
                            throw new ArgumentException("#2 Invalid Unit of measure" + match.Groups[4].Value);
                        }
                        return(new Measure(whole + (n1 / n2), unit));
                    }
                    else if (measureStr.ToUpper().Trim() == "N/A")
                    {
                        return(Measure.Empty);
                    }
                    else
                    {
                        double cm = 0;
                        if (double.TryParse(measureStr, out cm))
                        {
                            return(Measure.FromNormalizedValue(cm, null));
                        }
                    }
                }
            }
            return(base.ConvertFrom(context, culture, value));
        }