Пример #1
0
        public static bool TryParse(
            string txt,
            QualifiedNumber qn)
        {
            double d;

            if (txt == null)
            {
                return(false);
            }

            ParseToTextElements(txt, StaticQnte);
            qn.Qualifier = StaticQnte.Qualifier;

            if (NumberEx.DoubleTryParseEx(StaticQnte.NumberValue, out d))
            {
                qn.NumberValue = d;
            }

            else
            {
                return(false);
            }

            // todo: finish parsing stats

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Compare two objects
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>

        public static int Compare(
            object o1,
            object o2)
        {
            if (o1 == null || o2 == null)
            {
                throw new ArgumentNullException("Null argument(s)");
            }

            Type t1 = o1.GetType();
            Type t2 = o2.GetType();

            IComparable c1 = o1 as IComparable;

            if (c1 == null)
            {
                throw new InvalidDataException("Type doesn't support IComparable: " + t1);
            }

            IComparable c2 = o2 as IComparable;

            if (c2 == null)
            {
                throw new InvalidDataException("Type doesn't support IComparable: " + t2);
            }

            if (t1.Equals(t2))
            {
                return(c1.CompareTo(c2));                           // same type?
            }
            else if (t1.IsAssignableFrom(t2))
            {
                return(c1.CompareTo(c2));                                          // t1 subclass of t2
            }
            else if (t2.IsAssignableFrom(t1))
            {
                return(-c2.CompareTo(c1));                           // t2 subclass of t1 (reverse order)
            }
            else if (NumberEx.IsNumber(o1) && NumberEx.IsNumber(o2)) // two primitive numbers?
            {
                double d1 = unchecked ((double)o1);                  // convert to double ignoring any overflow
                double d2 = unchecked ((double)o2);
                return(d1.CompareTo(d2));
            }

            else             // see if either arg is a MobiusDataType
            {
                Type mdt = typeof(MobiusDataType);
                if (mdt.IsAssignableFrom(t1))
                {
                    return(c1.CompareTo(c2));                                          // t1 is MobiusDataType
                }
                else if (mdt.IsAssignableFrom(t2))
                {
                    return(-c2.CompareTo(c1));                                               // t2 is MobiusDataType (reverse order)
                }
                else
                {
                    throw new ArgumentException("Types not comparable: " + t1 + ", " + t2);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Compare two values (IComparable.CompareTo)
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>

        public override int CompareTo(
            object o)
        {
// First convert o to a double value

            double compVal = NullNumber;

            if (o == null)
            {
                compVal = NullNumber;
            }

            else if (o is int)
            {
                compVal = (int)o;
            }

            else if (o is long)
            {
                compVal = (long)o;
            }

            else if (o is float)
            {
                compVal = (float)o;
            }

            else if (o is double)
            {
                compVal = (double)o;
            }

            else if (o is decimal)
            {
                compVal = decimal.ToDouble((decimal)o);
            }

            else if (o is double)
            {
                compVal = (double)o;
            }

            else if (o is string)
            {
                if (!NumberEx.DoubleTryParseEx((string)o, out compVal))
                {
                    compVal = NullNumber;
                }
            }

            else if (o is StringMx)
            {
                StringMx sx = o as StringMx;
                if (!NumberEx.DoubleTryParseEx(sx?.Value, out compVal))
                {
                    compVal = NullNumber;
                }
            }

            else if (o is MobiusDataType)
            {
                compVal = ((MobiusDataType)o).NumericValue;
            }

            else
            {
                throw new Exception("Can't compare a " + GetType().Name + " to a " + o.GetType());
            }

// Now compare compVal to this NumberMx value

            if (this.Value != NullNumber && compVal != NullNumber)             // both not null
            {
                return(this.Value.CompareTo(compVal));
            }

            else if (this.Value == NullNumber && compVal == NullNumber) // both null
            {
                return(0);                                              // say equal
            }
            else if (this.Value != NullNumber)                          // this not null, compValue is null
            {
                return(-1);                                             // indicate this is less (put non-null first)
            }
            else                                                        // compValue not null, this is null
            {
                return(1);                                              // indicate that this is greater (put non-null first)
            }
        }