StringToNumber() public static method

public static StringToNumber ( string number ) : object
number string
return object
コード例 #1
0
ファイル: RangeQueryParser.cs プロジェクト: stgwilli/ravendb
        /// <summary>
        /// Detects numeric range terms and expands range expressions accordingly
        /// </summary>
        /// <param name="field"></param>
        /// <param name="lower"></param>
        /// <param name="upper"></param>
        /// <param name="inclusive"></param>
        /// <returns></returns>
        protected override Query GetRangeQuery(string field, string lower, string upper, bool inclusive)
        {
            if (!rangeValue.IsMatch(lower) && !rangeValue.IsMatch(upper))
            {
                return(base.GetRangeQuery(field, lower, upper, inclusive));
            }

            var from = NumberUtil.StringToNumber(lower);
            var to   = NumberUtil.StringToNumber(upper);

            TypeCode numericType;

            if (from != null)
            {
                numericType = Type.GetTypeCode(from.GetType());
            }
            else if (to != null)
            {
                numericType = Type.GetTypeCode(to.GetType());
            }
            else
            {
                numericType = TypeCode.Empty;
            }

            switch (numericType)
            {
            case TypeCode.Int32:
            {
                return(NumericRangeQuery.NewIntRange(field, (int)(from ?? Int32.MinValue), (int)(to ?? Int32.MaxValue), inclusive, inclusive));
            }

            case TypeCode.Int64:
            {
                return(NumericRangeQuery.NewLongRange(field, (long)(from ?? Int64.MinValue), (long)(to ?? Int64.MaxValue), inclusive, inclusive));
            }

            case TypeCode.Double:
            {
                return(NumericRangeQuery.NewDoubleRange(field, (double)(from ?? Double.MinValue), (double)(to ?? Double.MaxValue), inclusive, inclusive));
            }

            case TypeCode.Single:
            {
                return(NumericRangeQuery.NewFloatRange(field, (float)(from ?? Single.MinValue), (float)(to ?? Single.MaxValue), inclusive, inclusive));
            }

            default:
            {
                return(base.GetRangeQuery(field, lower, upper, inclusive));
            }
            }
        }
コード例 #2
0
        /// <summary>
        /// Detects numeric range terms and expands range expressions accordingly
        /// </summary>
        /// <param name="field"></param>
        /// <param name="lower"></param>
        /// <param name="upper"></param>
        /// <param name="inclusive"></param>
        /// <returns></returns>
        protected override Query GetRangeQuery(string field, string lower, string upper, bool inclusive)
        {
            bool minInclusive = inclusive;
            bool maxInclusive = inclusive;

            if (lower == "NULL" || lower == "*")
            {
                lower        = null;
                minInclusive = true;
            }
            if (upper == "NULL" || upper == "*")
            {
                upper        = null;
                maxInclusive = true;
            }

            if ((lower == null || !NumericRangeValue.IsMatch(lower)) && (upper == null || !NumericRangeValue.IsMatch(upper)))
            {
                return(NewRangeQuery(field, lower, upper, inclusive));
            }

            var from = NumberUtil.StringToNumber(lower);
            var to   = NumberUtil.StringToNumber(upper);

            TypeCode numericType;

            if (from != null)
            {
                numericType = Type.GetTypeCode(from.GetType());
            }
            else if (to != null)
            {
                numericType = Type.GetTypeCode(to.GetType());
            }
            else
            {
                numericType = TypeCode.Empty;
            }

            switch (numericType)
            {
            case TypeCode.Int32:
            {
                return(NumericRangeQuery.NewIntRange(field, (int)(from ?? Int32.MinValue), (int)(to ?? Int32.MaxValue), minInclusive, maxInclusive));
            }

            case TypeCode.Int64:
            {
                return(NumericRangeQuery.NewLongRange(field, (long)(from ?? Int64.MinValue), (long)(to ?? Int64.MaxValue), minInclusive, maxInclusive));
            }

            case TypeCode.Double:
            {
                return(NumericRangeQuery.NewDoubleRange(field, (double)(from ?? Double.MinValue), (double)(to ?? Double.MaxValue), minInclusive, maxInclusive));
            }

            case TypeCode.Single:
            {
                return(NumericRangeQuery.NewFloatRange(field, (float)(from ?? Single.MinValue), (float)(to ?? Single.MaxValue), minInclusive, maxInclusive));
            }

            default:
            {
                return(NewRangeQuery(field, lower, upper, inclusive));
            }
            }
        }