Example #1
0
        public int ToInteger()
        {
            if (this._arg is int i)
            {
                return(i);
            }

            if (this._arg is double d)
            {
                return((int)d);
            }

            if (this._arg is string s)
            {
                try
                {
                    return(int.Parse(s));
                }
                catch (Exception)
                {
                    // Allow to pass through to ArgumentCastException
                }
            }

            throw ArgumentCastException.Create("integer", this);
        }
Example #2
0
        public double ToDouble()
        {
            if (this._arg is double d)
            {
                return(d);
            }

            if (this._arg is float f)
            {
                return(f);
            }

            if (this._arg is int i)
            {
                return(i);
            }

            if (this._arg is string s)
            {
                try
                {
                    return(double.Parse(s));
                }
                catch (Exception)
                {
                    // Allow to pass through to ArgumentCastException
                }
            }

            throw ArgumentCastException.Create("double", this);
        }
Example #3
0
        public int ToTime()
        {
            if (IsInteger)
            {
                return(ToInteger());
            }

            if (this._arg is string s)
            {
                return((int)DateUtils.MultiParseTime(s));
            }

            throw ArgumentCastException.Create("time", this);
        }
Example #4
0
        public override Argument VisitSgnExpression(CalcParser.SgnExpressionContext context)
        {
            Argument arg = Visit(context.expression());

            if (arg.IsNull)
            {
                return(Argument.Null);
            }

            if (arg.IsDouble)
            {
                return(new Argument(Math.Sign(arg.ToDouble())));
            }

            throw ArgumentCastException.Create("double", arg);
        }
Example #5
0
        public DateTime?ToDate()
        {
            if (this._arg is DateTime dateTime)
            {
                return(dateTime);
            }

            if (this._arg is string s)
            {
                DateTime?date = DateUtils.MultiParseDate(s);
                if (date != null)
                {
                    return(date);
                }
            }

            throw ArgumentCastException.Create("date", this);
        }
Example #6
0
        public bool ToBoolean()
        {
            if (this._arg is bool b)
            {
                return(b);
            }

            if (this._arg is string s)
            {
                if (s.Equals("true", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                if (s.Equals("false", StringComparison.OrdinalIgnoreCase))
                {
                    return(false);
                }
            }

            throw ArgumentCastException.Create("boolean", this);
        }