Exemplo n.º 1
0
        public virtual object ConvertToNumber(object value)
        {
            var type = NumericType;

            if (type.IsInstanceOfType(value))
            {
                return(value);
            }

            if (value == null)
            {
                return(ConvertToNumber(0));
            }

            if (value is string)
            {
                if (type.Equals(typeof(Decimal)))
                {
                    return(Decimal.Parse((string)value, AutoParseNumericStringsFormatProvider ?? CultureInfo.InvariantCulture));
                }

                if (type.Equals(typeof(Double)))
                {
                    return((double)StringConversion.OptionallyConvertStringToDouble(value, AutoParseNumericStringsFormatProvider));
                }

                if (type.Equals(typeof(Single)))
                {
                    return((float)(double)StringConversion.OptionallyConvertStringToDouble(value, AutoParseNumericStringsFormatProvider));
                }

                if (type.Equals(typeof(Int64)))
                {
                    return(Int64.Parse((string)value, AutoParseNumericStringsFormatProvider ?? CultureInfo.InvariantCulture));
                }

                if (type.Equals(typeof(Int32)))
                {
                    return(Int32.Parse((string)value, AutoParseNumericStringsFormatProvider ?? CultureInfo.InvariantCulture));
                }
            }

            return(Convert.ChangeType(value, type));
        }
Exemplo n.º 2
0
        public static Dictionary <string, EvalFunctionDelegate> GetDefaultGenericFunctions(
            Type type, bool autoParseNumericStrings = true, IFormatProvider autoParseNumericStringsFormatProvider = null)
        {
            Func <object, object> argFilter;

            if (autoParseNumericStrings)
            {
                argFilter = arg =>
                {
                    return(StringConversion.OptionallyConvertStringToDouble(arg, autoParseNumericStringsFormatProvider));
                };
            }
            else
            {
                argFilter = arg => arg;
            }

            return(new Dictionary <string, EvalFunctionDelegate>
            {
                { "ABS", args => Convert.ChangeType(Math.Abs((dynamic)argFilter(args[0])), type) },
                { "ACOS", args => Convert.ChangeType(Math.Acos(Convert.ToDouble(argFilter(args[0]))), type) },
                { "ASIN", args => Convert.ChangeType(Math.Asin(Convert.ToDouble(argFilter(args[0]))), type) },
                { "ATAN", args => Convert.ChangeType(Math.Atan(Convert.ToDouble(argFilter(args[0]))), type) },
                { "ATAN2", args => Convert.ChangeType(Math.Atan2(Convert.ToDouble(argFilter(args[0])), Convert.ToDouble(argFilter(args[1]))), type) },
                { "CEILING", args => {
                      var arg = args[0];
                      if (arg is double)
                      {
                          return Convert.ChangeType(Math.Ceiling((double)arg), type);
                      }
                      if (arg is decimal)
                      {
                          return Convert.ChangeType(Math.Ceiling((decimal)arg), type);
                      }
                      return Convert.ChangeType(Math.Ceiling(Convert.ToDouble(argFilter(arg))), type);
                  } },
                { "COS", args => Convert.ChangeType(Math.Cos(Convert.ToDouble(argFilter(args[0]))), type) },
                { "COSH", args => Convert.ChangeType(Math.Cosh(Convert.ToDouble(argFilter(args[0]))), type) },
                { "EXP", args => Convert.ChangeType(Math.Exp(Convert.ToDouble(argFilter(args[0]))), type) },
                { "FLOOR", args => {
                      var arg = args[0];
                      if (arg is double)
                      {
                          return Convert.ChangeType(Math.Floor((double)arg), type);
                      }
                      if (arg is decimal)
                      {
                          return Convert.ChangeType(Math.Floor((decimal)arg), type);
                      }
                      return Convert.ChangeType(Math.Floor(Convert.ToDouble(argFilter(arg))), type);
                  } },
                { "LOG", args => {
                      if (args.Length == 2)
                      {
                          return Convert.ChangeType(Math.Log(Convert.ToDouble(argFilter(args[0])), Convert.ToDouble(argFilter(args[1]))), type);
                      }
                      return Convert.ChangeType(Math.Log(Convert.ToDouble(argFilter(args[0]))), type);
                  } },
                { "LOG2", args => Convert.ChangeType(Math.Log(Convert.ToDouble(argFilter(args[0])), 2), type) },
                { "LOG10", args => Convert.ChangeType(Math.Log10(Convert.ToDouble(argFilter(args[0]))), type) },
                { "MAX", args => {
                      if (args.Length == 0)
                      {
                          return null;
                      }
                      else
                      {
                          dynamic v = argFilter(args[0]);
                          if (v == null)
                          {
                              return null;
                          }
                          for (var i = 0; i < args.Length; i++)
                          {
                              dynamic v2 = argFilter(args[i]);
                              if (v2 == null)
                              {
                                  return null;
                              }
                              if (v2.CompareTo(v) > 0)
                              {
                                  v = v2;
                              }
                          }
                          return v;
                      }
                  } },
                { "MIN", args => {
                      if (args.Length == 0)
                      {
                          return null;
                      }
                      else
                      {
                          dynamic v = argFilter(args[0]);
                          if (v == null)
                          {
                              return null;
                          }
                          for (var i = 0; i < args.Length; i++)
                          {
                              dynamic v2 = argFilter(args[i]);
                              if (v2 == null)
                              {
                                  return null;
                              }
                              if (v2.CompareTo(v) < 0)
                              {
                                  v = v2;
                              }
                          }
                          return v;
                      }
                  } },
                { "POW", args => Convert.ChangeType(Math.Pow(Convert.ToDouble(argFilter(args[0])), Convert.ToDouble(argFilter(args[1]))), type) },
                { "ROUND", args => {
                      var arg = args[0];
                      if (arg is double)
                      {
                          return Convert.ChangeType(Math.Round((double)arg), type);
                      }
                      if (arg is decimal)
                      {
                          return Convert.ChangeType(Math.Round((decimal)arg), type);
                      }
                      return Convert.ChangeType(Math.Round(Convert.ToDouble(argFilter(arg))), type);
                  } },
                { "SIGN", args => Convert.ChangeType(Math.Sign((dynamic)argFilter(args[0])), type) },
                { "SIN", args => Convert.ChangeType(Math.Sin(Convert.ToDouble(argFilter(args[0]))), type) },
                { "SINH", args => Convert.ChangeType(Math.Sinh(Convert.ToDouble(argFilter(args[0]))), type) },
                { "SQRT", args => Convert.ChangeType(Math.Sqrt(Convert.ToDouble(argFilter(args[0]))), type) },
                { "TAN", args => Convert.ChangeType(Math.Tan(Convert.ToDouble(argFilter(args[0]))), type) },
                { "TANH", args => Convert.ChangeType(Math.Tanh(Convert.ToDouble(argFilter(args[0]))), type) },
                { "TRUNCATE", args => {
                      var arg = args[0];
                      if (arg is double)
                      {
                          return Convert.ChangeType(Math.Truncate((double)arg), type);
                      }
                      if (arg is decimal)
                      {
                          return Convert.ChangeType(Math.Truncate((decimal)arg), type);
                      }
                      return Convert.ChangeType(Math.Truncate(Convert.ToDouble(argFilter(arg))), type);
                  } },
            });
        }