StringToNumber() 공개 정적인 메소드

Converts string into integer, long integer and double value using conversion algorithm in a manner of PHP.
is a null reference.
public static StringToNumber ( string str, long &longValue, double &doubleValue ) : NumberInfo
str string The string to convert.
longValue long The result of conversion to long integer.
doubleValue double The result of conversion to double.
리턴 NumberInfo
예제 #1
0
        /// <summary>
        /// Compares string in a manner of PHP.
        /// </summary>
        /// <remarks>Note that this comparison is not transitive (e.g. {"2","10","10a"} leads to a contradiction).</remarks>
        public static int Compare(string /*!*/ sx, string /*!*/ sy)
        {
            Debug.Assert(sx != null);
            Debug.Assert(sy != null);

            long   lx, ly;
            double dx, dy;

            Convert.NumberInfo info_x, info_y;

            info_x = Convert.StringToNumber(sx, out lx, out dx);

            // an operand is not entirely convertable to numbers => string comparison is performed:
            if ((info_x & Convert.NumberInfo.IsNumber) == 0)
            {
                return(string.CompareOrdinal(sx, sy));
            }

            info_y = Convert.StringToNumber(sy, out ly, out dy);

            // an operand is not entirely convertable to numbers => string comparison is performed:
            if ((info_y & Convert.NumberInfo.IsNumber) == 0)
            {
                return(string.CompareOrdinal(sx, sy));
            }

            // numeric comparison
            return((((info_x | info_y) & Convert.NumberInfo.Double) != 0)
                ? Compare(dx, dy)   // at least one operand has been converted to double:
                : Compare(lx, ly)); // compare integers:
        }
예제 #2
0
        public PhpNumber ToNumber()
        {
            double d;
            long   l;
            var    info = Convert.StringToNumber(ToString(), out l, out d);

            return((info & Convert.NumberInfo.Double) != 0 ? PhpNumber.Create(d) : PhpNumber.Create(l));
        }
예제 #3
0
        public Convert.NumberInfo ToNumber(out PhpNumber number)
        {
            double d;
            long   l;
            var    info = Convert.StringToNumber(ToString(), out l, out d);

            number = (info & Convert.NumberInfo.Double) != 0
                ? PhpNumber.Create(d)
                : PhpNumber.Create(l);

            return(info);
        }
예제 #4
0
        /// <summary>
        /// Compares two objects for equality in a manner of the PHP regular comparison.
        /// </summary>
        /// <param name="x">The first object.</param>
        /// <param name="dy">The second object.</param>
        /// <returns>Whether the values of operands are the same.</returns>
        public static bool Equals(string /*!*/ x, double dy)
        {
            Debug.Assert(x != null);

            double dx;
            long   lx;

            switch (Convert.StringToNumber(x, out lx, out dx) & Convert.NumberInfo.TypeMask)
            {
            case Convert.NumberInfo.Double: return(dx == dy);

            case Convert.NumberInfo.LongInteger: return(lx == dy);

            default: Debug.Assert(false); throw null;
            }
        }
예제 #5
0
        /// <summary>
        /// Compares a <see cref="string"/> with <see cref="double"/>.
        /// </summary>
        public static int Compare(string /*!*/ sx, double dy)
        {
            Debug.Assert(sx != null);

            double dx;
            long   lx;

            switch (Convert.StringToNumber(sx, out lx, out dx) & Convert.NumberInfo.TypeMask)
            {
            case Convert.NumberInfo.Double: return(Compare(dx, dy));

            case Convert.NumberInfo.LongInteger: return(Compare((double)lx, dy));

            default: Debug.Assert(false); throw null;
            }
        }
예제 #6
0
        /// <summary>
        /// Compares string in a manner of PHP.
        /// </summary>
        /// <remarks>Note that this comparison is not transitive (e.g. {"2","10","10a"} leads to a contradiction).</remarks>
        public static int Compare(string sx, string sy)
        {
            var info_x = Convert.StringToNumber(sx, out var lx, out var dx);

            // an operand is not entirely convertable to numbers => string comparison is performed:
            if ((info_x & Convert.NumberInfo.IsNumber) == 0)
            {
                return(string.CompareOrdinal(sx, sy));
            }

            var info_y = Convert.StringToNumber(sy, out var ly, out var dy);

            // an operand is not entirely convertable to numbers => string comparison is performed:
            if ((info_y & Convert.NumberInfo.IsNumber) == 0)
            {
                return(string.CompareOrdinal(sx, sy));
            }

            // numeric comparison
            return((((info_x | info_y) & Convert.NumberInfo.Double) != 0)
                ? Compare(dx, dy)   // at least one operand has been converted to double:
                : Compare(lx, ly)); // compare integers:
        }
예제 #7
0
        /// <summary>
        /// Compares a <see cref="string"/> with <see cref="long"/>.
        /// </summary>
        public static int Compare(string /*!*/ sx, long ly)
        {
            Debug.Assert(sx != null);

            switch (Convert.StringToNumber(sx, out var lx, out var dx) & Convert.NumberInfo.TypeMask)
            {