System.IConvertible.ToDecimal(System.IFormatProvider)

Here are the examples of the csharp api System.IConvertible.ToDecimal(System.IFormatProvider) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

50 Examples 7

19 Source : CompressedString.cs
with MIT License
from circles-arrows

decimal IConvertible.ToDecimal(IFormatProvider? provider)
        {
            return ((IConvertible)Value).ToDecimal(provider);
        }

19 Source : JsonConvert.cs
with MIT License
from CragonGame

public static string ToString(object value)
    {
      if (value == null)
        return Null;

      IConvertible convertible = value as IConvertible;

      if (convertible != null)
      {
        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            return ToString(convertible.ToString(CultureInfo.InvariantCulture));
          case TypeCode.Char:
            return ToString(convertible.ToChar(CultureInfo.InvariantCulture));
          case TypeCode.Boolean:
            return ToString(convertible.ToBoolean(CultureInfo.InvariantCulture));
          case TypeCode.SByte:
            return ToString(convertible.ToSByte(CultureInfo.InvariantCulture));
          case TypeCode.Int16:
            return ToString(convertible.ToInt16(CultureInfo.InvariantCulture));
          case TypeCode.UInt16:
            return ToString(convertible.ToUInt16(CultureInfo.InvariantCulture));
          case TypeCode.Int32:
            return ToString(convertible.ToInt32(CultureInfo.InvariantCulture));
          case TypeCode.Byte:
            return ToString(convertible.ToByte(CultureInfo.InvariantCulture));
          case TypeCode.UInt32:
            return ToString(convertible.ToUInt32(CultureInfo.InvariantCulture));
          case TypeCode.Int64:
            return ToString(convertible.ToInt64(CultureInfo.InvariantCulture));
          case TypeCode.UInt64:
            return ToString(convertible.ToUInt64(CultureInfo.InvariantCulture));
          case TypeCode.Single:
            return ToString(convertible.ToSingle(CultureInfo.InvariantCulture));
          case TypeCode.Double:
            return ToString(convertible.ToDouble(CultureInfo.InvariantCulture));
          case TypeCode.DateTime:
            return ToString(convertible.ToDateTime(CultureInfo.InvariantCulture));
          case TypeCode.Decimal:
            return ToString(convertible.ToDecimal(CultureInfo.InvariantCulture));
          case TypeCode.DBNull:
            return Null;
        }
      }
      else if (value is DateTimeOffset)
      {
        return ToString((DateTimeOffset)value);
      }
      else if (value is Guid)
      {
        return ToString((Guid) value);
      }
      else if (value is Uri)
      {
        return ToString((Uri) value);
      }
      else if (value is TimeSpan)
      {
        return ToString((TimeSpan) value);
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : JsonWriter.cs
with MIT License
from CragonGame

public virtual void WriteValue(object value)
    {
      if (value == null)
      {
        WriteNull();
        return;
      }
      else if (value is IConvertible)
      {
        IConvertible convertible = value as IConvertible;

        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Char:
            WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Boolean:
            WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
            return;
          case TypeCode.SByte:
            WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int16:
            WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt16:
            WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int32:
            WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Byte:
            WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt32:
            WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int64:
            WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt64:
            WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Single:
            WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Double:
            WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DateTime:
            WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Decimal:
            WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DBNull:
            WriteNull();
            return;
        }
      }
      else if (value is DateTimeOffset)
      {
        WriteValue((DateTimeOffset)value);
        return;
      }
      else if (value is byte[])
      {
        WriteValue((byte[])value);
        return;
      }
      else if (value is Guid)
      {
        WriteValue((Guid)value);
        return;
      }
      else if (value is Uri)
      {
        WriteValue((Uri)value);
        return;
      }
      else if (value is TimeSpan)
      {
        WriteValue((TimeSpan)value);
        return;
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : SYSTEMTIME.cs
with MIT License
from dahall

decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)(DateTime)this).ToDecimal(provider);

19 Source : BOOLEAN.cs
with MIT License
from dahall

decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider);

19 Source : HRESULT.cs
with MIT License
from dahall

decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider);

19 Source : Win32Error.cs
with MIT License
from dahall

decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)value).ToDecimal(provider);

19 Source : HRESULTTests.cs
with MIT License
from dahall

[Test()]
		public void IConvTest()
		{
			HRESULT hr = HRESULT.E_ACCESSDENIED;
			var c = (IConvertible) hr;
			var cv = (IConvertible)HRESULT.E_ACCESSDENIED;
			var f = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
			replacedert.That(c.GetTypeCode(), Is.EqualTo(cv.GetTypeCode()));
			replacedert.That(() => c.ToChar(f), Throws.Exception);
			replacedert.That(() => c.ToSByte(f), Throws.Exception);
			replacedert.That(() => c.ToByte(f), Throws.Exception);
			replacedert.That(() => c.ToInt16(f), Throws.Exception);
			replacedert.That(() => c.ToUInt16(f), Throws.Exception);
			replacedert.That(c.ToUInt32(f), Is.EqualTo(unchecked((uint)HRESULT.E_ACCESSDENIED)));
			replacedert.That(c.ToInt32(f), Is.EqualTo(cv.ToInt32(f)));
			replacedert.That(c.ToInt64(f), Is.EqualTo(cv.ToInt64(f)));
			replacedert.That(c.ToUInt64(f), Is.EqualTo((ulong)unchecked((uint)HRESULT.E_ACCESSDENIED)));
			replacedert.That(c.ToSingle(f), Is.EqualTo(cv.ToSingle(f)));
			replacedert.That(c.ToDouble(f), Is.EqualTo(cv.ToDouble(f)));
			replacedert.That(c.ToDecimal(f), Is.EqualTo(cv.ToDecimal(f)));
			replacedert.That(() => c.ToDateTime(f), Throws.Exception);
			replacedert.That(c.ToString(f), Does.StartWith("E_ACCESSDENIED"));
			replacedert.That(c.ToType(typeof(int), f), Is.EqualTo(cv.ToType(typeof(int), f)));
		}

19 Source : Win32ErrorTests.cs
with MIT License
from dahall

[Test()]
		public void IConvTest()
		{
			Win32Error err = Win32Error.ERROR_ACCESS_DENIED;
			var c = (IConvertible)err;
			var cv = (IConvertible)Win32Error.ERROR_ACCESS_DENIED;
			var f = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
			replacedert.That(c.GetTypeCode(), Is.EqualTo(cv.GetTypeCode()));
			replacedert.That(() => c.ToChar(f), Throws.Exception);
			replacedert.That(c.ToSByte(f), Is.EqualTo(cv.ToSByte(f)));
			replacedert.That(c.ToByte(f), Is.EqualTo(cv.ToByte(f)));
			replacedert.That(c.ToInt16(f), Is.EqualTo(cv.ToInt16(f)));
			replacedert.That(c.ToUInt16(f), Is.EqualTo(cv.ToUInt16(f)));
			replacedert.That(c.ToInt32(f), Is.EqualTo(cv.ToInt32(f)));
			replacedert.That(c.ToUInt32(f), Is.EqualTo(cv.ToUInt32(f)));
			replacedert.That(c.ToInt64(f), Is.EqualTo(cv.ToInt64(f)));
			replacedert.That(c.ToUInt64(f), Is.EqualTo(cv.ToUInt64(f)));
			replacedert.That(c.ToSingle(f), Is.EqualTo(cv.ToSingle(f)));
			replacedert.That(c.ToDouble(f), Is.EqualTo(cv.ToDouble(f)));
			replacedert.That(c.ToDecimal(f), Is.EqualTo(cv.ToDecimal(f)));
			replacedert.That(() => c.ToDateTime(f), Throws.Exception);
			replacedert.That(c.ToString(f), Does.StartWith("ERROR_ACCESS_DENIED"));
			replacedert.That(c.ToType(typeof(uint), f), Is.EqualTo(cv.ToType(typeof(uint), f)));
		}

19 Source : NTStatusTests.cs
with MIT License
from dahall

[Test()]
		public void IConvTest()
		{
			NTStatus nts = NTStatus.STATUS_ACCESS_DENIED;
			var c = (IConvertible)nts;
			var cv = (IConvertible)NTStatus.STATUS_ACCESS_DENIED;
			var f = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
			replacedert.That(c.GetTypeCode(), Is.EqualTo(cv.GetTypeCode()));
			replacedert.That(() => c.ToChar(f), Throws.Exception);
			replacedert.That(() => c.ToSByte(f), Throws.Exception);
			replacedert.That(() => c.ToByte(f), Throws.Exception);
			replacedert.That(() => c.ToInt16(f), Throws.Exception);
			replacedert.That(() => c.ToUInt16(f), Throws.Exception);
			replacedert.That(c.ToInt32(f), Is.EqualTo(cv.ToInt32(f)));
			replacedert.That(c.ToUInt32(f), Is.EqualTo((uint)nts));
			replacedert.That(c.ToInt64(f), Is.EqualTo(cv.ToInt64(f)));
			replacedert.That(c.ToUInt64(f), Is.EqualTo((ulong)(uint)nts));
			replacedert.That(c.ToSingle(f), Is.EqualTo(cv.ToSingle(f)));
			replacedert.That(c.ToDouble(f), Is.EqualTo(cv.ToDouble(f)));
			replacedert.That(c.ToDecimal(f), Is.EqualTo(cv.ToDecimal(f)));
			replacedert.That(c.ToBoolean(f), Is.EqualTo(nts.Succeeded));
			replacedert.That(() => c.ToDateTime(f), Throws.Exception);
			replacedert.That(c.ToString(f), Does.StartWith("STATUS_ACCESS_DENIED"));
			replacedert.That(c.ToType(typeof(int), f), Is.EqualTo(cv.ToType(typeof(int), f)));
		}

19 Source : AseDataReader.cs
with Apache License 2.0
from DataAction

public override decimal GetDecimal(int i)
        {
            return GetPrimitive(i, (value, provider) => value.ToDecimal(provider));
        }

19 Source : SqlNative.cs
with MIT License
from DataObjects-NET

decimal IConvertible.ToDecimal(IFormatProvider provider)
    {
      return ((IConvertible) Value).ToDecimal(provider);
    }

19 Source : AbapStringValue.cs
with MIT License
from dbosoft

public decimal ToDecimal(IFormatProvider provider)
        {
            return ((IConvertible) Value).ToDecimal(CultureInfo.InvariantCulture);
        }

19 Source : AbapIntValue.cs
with MIT License
from dbosoft

public decimal ToDecimal(IFormatProvider provider)
        {
            return ((IConvertible) Value).ToDecimal(provider);
        }

19 Source : OracleDataReader.cs
with MIT License
from ericmend

public
        override
        decimal GetDecimal(int i)
        {
            IConvertible c = (IConvertible)GetValue(i);
            return c.ToDecimal(CultureInfo.CurrentCulture);
        }

19 Source : Calculator.cs
with MIT License
from gimlichael

private static T CalculateCore<T>(T x, T y, replacedignmentOperator replacedignment) where T : struct, IConvertible
        {
            var provider = CultureInfo.InvariantCulture;
            var replacedignmentCode = x.GetTypeCode();
            switch (replacedignmentCode)
            {
                case TypeCode.Byte:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToByte(provider) + y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToByte(provider) & y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToByte(provider) / y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToByte(provider) ^ y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToByte(provider) << y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToByte(provider) * y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToByte(provider) | y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToByte(provider) % y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToByte(provider) >> y.ToByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToByte(provider) - y.ToByte(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Decimal:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) + y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x &= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) / y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x ^= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) * y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x |= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) % y.ToDecimal(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToDecimal(provider) - y.ToDecimal(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Double:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToDouble(provider) + y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x &= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToDouble(provider) / y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x ^= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToDouble(provider) * y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x |= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToDouble(provider) % y.ToDouble(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToDouble(provider) - y.ToDouble(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Int16:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToInt16(provider) + y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToInt16(provider) & y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToInt16(provider) / y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToInt16(provider) ^ y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToInt16(provider) << y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToInt16(provider) * y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToInt16(provider) | y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToInt16(provider) % y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToInt16(provider) >> y.ToInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToInt16(provider) - y.ToInt16(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Int32:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToInt32(provider) + y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToInt32(provider) & y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToInt32(provider) / y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToInt32(provider) ^ y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToInt32(provider) << y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToInt32(provider) * y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToInt32(provider) | y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToInt32(provider) % y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToInt32(provider) >> y.ToInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToInt32(provider) - y.ToInt32(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Int64:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToInt64(provider) + y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToInt64(provider) & y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToInt64(provider) / y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToInt64(provider) ^ y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToInt64(provider) * y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToInt64(provider) | y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToInt64(provider) % y.ToInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToInt64(provider) - y.ToInt64(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.SByte:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToSByte(provider) + y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToSByte(provider) & y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToSByte(provider) / y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToSByte(provider) ^ y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToSByte(provider) << y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToSByte(provider) * y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToSByte(provider) | y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToSByte(provider) % y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToSByte(provider) >> y.ToSByte(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToSByte(provider) - y.ToSByte(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.Single:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToSingle(provider) + y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x &= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToSingle(provider) / y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x ^= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToSingle(provider) * y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x |= y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToSingle(provider) % y.ToSingle(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToSingle(provider) - y.ToSingle(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.UInt16:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) + y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) & y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) / y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) ^ y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) << y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) * y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) | y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) % y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) >> y.ToUInt16(provider)).ChangeType<T>();
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToUInt16(provider) - y.ToUInt16(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.UInt32:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) + y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) & y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) / y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) ^ y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) * y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) | y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) % y.ToUInt32(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToUInt32(provider) - y.ToUInt32(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                case TypeCode.UInt64:
                    switch (replacedignment)
                    {
                        case replacedignmentOperator.Addition:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) + y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.And:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) & y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.replacedign:
                            return y;
                        case replacedignmentOperator.Division:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) / y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.ExclusiveOr:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) ^ y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.LeftShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x << y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Multiplication:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) * y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Or:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) | y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.Remainder:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) % y.ToUInt64(provider)).ChangeType<T>();
                        case replacedignmentOperator.RightShift:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment), string.Format(CultureInfo.InvariantCulture, "Cannot apply replacedignment operator '{0}' (x >> y) to operands of type '{1}'.", Enum.GetName(typeof(replacedignmentOperator), replacedignment), typeof(T).Name));
                        case replacedignmentOperator.Subtraction:
                            return Decorator.Enclose<object>(x.ToUInt64(provider) - y.ToUInt64(provider)).ChangeType<T>();
                        default:
                            throw new ArgumentOutOfRangeException(nameof(replacedignment));
                    }
                default:
                    throw new TypeArgumentException("T", string.Format(CultureInfo.InvariantCulture, "T appears to contain an invalid type. Expected type is numeric and must be one of the following: Byte, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32 or UInt64. Actually type was {0}.", typeof(T).Name));
            }
        }

19 Source : bool.cs
with MIT License
from GridProtectionAlliance

decimal IConvertible.ToDecimal(IFormatProvider? provider) => ((IConvertible)Value).ToDecimal(provider);

19 Source : string.cs
with MIT License
from GridProtectionAlliance

decimal IConvertible.ToDecimal(IFormatProvider? provider) => ((IConvertible)ToString()).ToDecimal(provider);

19 Source : complex64.cs
with MIT License
from GridProtectionAlliance

decimal IConvertible.ToDecimal(IFormatProvider? provider) => ((IConvertible)m_real).ToDecimal(provider);

19 Source : JsonConvert.cs
with Apache License 2.0
from intuit

public static string ToString(object value)
    {
      if (value == null)
        return Null;

      IConvertible convertible = value as IConvertible;

      if (convertible != null)
      {
        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            return ToString(convertible.ToString(CultureInfo.InvariantCulture));
          case TypeCode.Char:
            return ToString(convertible.ToChar(CultureInfo.InvariantCulture));
          case TypeCode.Boolean:
            return ToString(convertible.ToBoolean(CultureInfo.InvariantCulture));
          case TypeCode.SByte:
            return ToString(convertible.ToSByte(CultureInfo.InvariantCulture));
          case TypeCode.Int16:
            return ToString(convertible.ToInt16(CultureInfo.InvariantCulture));
          case TypeCode.UInt16:
            return ToString(convertible.ToUInt16(CultureInfo.InvariantCulture));
          case TypeCode.Int32:
            return ToString(convertible.ToInt32(CultureInfo.InvariantCulture));
          case TypeCode.Byte:
            return ToString(convertible.ToByte(CultureInfo.InvariantCulture));
          case TypeCode.UInt32:
            return ToString(convertible.ToUInt32(CultureInfo.InvariantCulture));
          case TypeCode.Int64:
            return ToString(convertible.ToInt64(CultureInfo.InvariantCulture));
          case TypeCode.UInt64:
            return ToString(convertible.ToUInt64(CultureInfo.InvariantCulture));
          case TypeCode.Single:
            return ToString(convertible.ToSingle(CultureInfo.InvariantCulture));
          case TypeCode.Double:
            return ToString(convertible.ToDouble(CultureInfo.InvariantCulture));
          case TypeCode.DateTime:
            return ToString(convertible.ToDateTime(CultureInfo.InvariantCulture));
          case TypeCode.Decimal:
            return ToString(convertible.ToDecimal(CultureInfo.InvariantCulture));
          case TypeCode.DBNull:
            return Null;
        }
      }
#if !PocketPC && !NET20
      else if (value is DateTimeOffset)
      {
        return ToString((DateTimeOffset) value);
      }
#endif
      else if (value is Guid)
      {
        return ToString((Guid) value);
      }
      else if (value is Uri)
      {
        return ToString((Uri) value);
      }
      else if (value is TimeSpan)
      {
        return ToString((TimeSpan) value);
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : JsonWriter.cs
with Apache License 2.0
from intuit

public virtual void WriteValue(object value)
    {
      if (value == null)
      {
        WriteNull();
        return;
      }
      else if (value is IConvertible)
      {
        IConvertible convertible = value as IConvertible;

        switch (convertible.GetTypeCode())
        {
          case TypeCode.String:
            WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Char:
            WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Boolean:
            WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
            return;
          case TypeCode.SByte:
            WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int16:
            WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt16:
            WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int32:
            WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Byte:
            WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt32:
            WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Int64:
            WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.UInt64:
            WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Single:
            WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Double:
            WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DateTime:
            WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
            return;
          case TypeCode.Decimal:
            WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
            return;
          case TypeCode.DBNull:
            WriteNull();
            return;
        }
      }
#if !PocketPC && !NET20
      else if (value is DateTimeOffset)
      {
        WriteValue((DateTimeOffset)value);
        return;
      }
#endif
      else if (value is byte[])
      {
        WriteValue((byte[])value);
        return;
      }
      else if (value is Guid)
      {
        WriteValue((Guid)value);
        return;
      }
      else if (value is Uri)
      {
        WriteValue((Uri)value);
        return;
      }
      else if (value is TimeSpan)
      {
        WriteValue((TimeSpan)value);
        return;
      }

      throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer clreplaced to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
    }

19 Source : PolymorphicEnum.cs
with GNU General Public License v3.0
from ME3Tweaks

decimal IConvertible.ToDecimal(IFormatProvider provider)
		{
			return Ordinal.ToDecimal(provider);
		}

19 Source : FourBitNumber.cs
with MIT License
from melanchall

decimal IConvertible.ToDecimal(IFormatProvider provider)
        {
            return ((IConvertible)_value).ToDecimal(provider);
        }

19 Source : DefaultConverter.cs
with MIT License
from meziantou

protected virtual bool TryConvert(object? input, IFormatProvider? provider, out decimal value)
        {
            if (IsNullOrEmptyString(input))
            {
                value = 0;
                return false;
            }

            if (!(input is string))
            {
                if (input is IConvertible ic)
                {
                    try
                    {
                        value = ic.ToDecimal(provider);
                        return true;
                    }
                    catch
                    {
                    }
                }
            }

            var styles = NumberStyles.Integer;
            var s = Convert.ToString(input, provider);
            if (NormalizeHexString(ref s))
            {
                styles |= NumberStyles.AllowHexSpecifier;
            }
            return decimal.TryParse(s, styles, provider, out value);
        }

19 Source : ColorString.cs
with MIT License
from microsoft

decimal IConvertible.ToDecimal( IFormatProvider provider )
        {
            return ((IConvertible) this.ToString()).ToDecimal( provider );
        }

19 Source : Number.cs
with Apache License 2.0
from NightOwl888

internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider? provider)
        {
            Debug.replacedert(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (ReferenceEquals(value!.GetType(), targetType))
            {
                return value;
            }

            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Boolean]))
                return value.ToBoolean(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Char]))
                return value.ToChar(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.SByte]))
                return value.ToSByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Byte]))
                return value.ToByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int16]))
                return value.ToInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt16]))
                return value.ToUInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int32]))
                return value.ToInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt32]))
                return value.ToUInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int64]))
                return value.ToInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt64]))
                return value.ToUInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Single]))
                return value.ToSingle(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Double]))
                return value.ToDouble(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Decimal]))
                return value.ToDecimal(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DateTime]))
                return value.ToDateTime(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.String]))
                return value.ToString(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Object]))
                return (object)value;
            // Need to special case Enum because typecode will be underlying type, e.g. Int32
            if (ReferenceEquals(targetType, EnumType))
                return (Enum)value;
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DBNull]))
                throw new InvalidCastException(SR.InvalidCast_DBNull);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Empty]))
                throw new InvalidCastException(SR.InvalidCast_Empty);

            throw new InvalidCastException(J2N.SR.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullName));
        }

19 Source : BsonDecimal128.cs
with MIT License
from putifeng

protected override decimal IConvertibleToDecimalImplementation(IFormatProvider provider)
        {
            return ((IConvertible)_value).ToDecimal(provider);
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Scalar(object val)
        {
            switch (val.GetType().GetTypeCode())
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#1>(UnmanagedMemoryBlock<#1>.FromPool(_buffer)) {[0] = ((IConvertible)val).To#1(CultureInfo.InvariantCulture)};
	            %
	            default:
		            throw new NotSupportedException();
#else

                case NPTypeCode.Boolean: return new ArraySlice<Boolean>(UnmanagedMemoryBlock<Boolean>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToBoolean(CultureInfo.InvariantCulture)};
                case NPTypeCode.Byte: return new ArraySlice<Byte>(UnmanagedMemoryBlock<Byte>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToByte(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int16: return new ArraySlice<Int16>(UnmanagedMemoryBlock<Int16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt16: return new ArraySlice<UInt16>(UnmanagedMemoryBlock<UInt16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int32: return new ArraySlice<Int32>(UnmanagedMemoryBlock<Int32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt32: return new ArraySlice<UInt32>(UnmanagedMemoryBlock<UInt32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int64: return new ArraySlice<Int64>(UnmanagedMemoryBlock<Int64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt64: return new ArraySlice<UInt64>(UnmanagedMemoryBlock<UInt64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.Char: return new ArraySlice<Char>(UnmanagedMemoryBlock<Char>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToChar(CultureInfo.InvariantCulture)};
                case NPTypeCode.Double: return new ArraySlice<Double>(UnmanagedMemoryBlock<Double>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDouble(CultureInfo.InvariantCulture)};
                case NPTypeCode.Single: return new ArraySlice<Single>(UnmanagedMemoryBlock<Single>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToSingle(CultureInfo.InvariantCulture)};
                case NPTypeCode.Decimal: return new ArraySlice<Decimal>(UnmanagedMemoryBlock<Decimal>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDecimal(CultureInfo.InvariantCulture)};
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Scalar(object val, NPTypeCode typeCode)
        {
            switch (typeCode)
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#1>(UnmanagedMemoryBlock<#1>.FromPool(_buffer)) {[0] = ((IConvertible)val).To#1(CultureInfo.InvariantCulture)};
	            %
	            default:
		            throw new NotSupportedException();
#else

                case NPTypeCode.Boolean: return new ArraySlice<Boolean>(UnmanagedMemoryBlock<Boolean>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToBoolean(CultureInfo.InvariantCulture)};
                case NPTypeCode.Byte: return new ArraySlice<Byte>(UnmanagedMemoryBlock<Byte>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToByte(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int16: return new ArraySlice<Int16>(UnmanagedMemoryBlock<Int16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt16: return new ArraySlice<UInt16>(UnmanagedMemoryBlock<UInt16>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt16(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int32: return new ArraySlice<Int32>(UnmanagedMemoryBlock<Int32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt32: return new ArraySlice<UInt32>(UnmanagedMemoryBlock<UInt32>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt32(CultureInfo.InvariantCulture)};
                case NPTypeCode.Int64: return new ArraySlice<Int64>(UnmanagedMemoryBlock<Int64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.UInt64: return new ArraySlice<UInt64>(UnmanagedMemoryBlock<UInt64>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToUInt64(CultureInfo.InvariantCulture)};
                case NPTypeCode.Char: return new ArraySlice<Char>(UnmanagedMemoryBlock<Char>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToChar(CultureInfo.InvariantCulture)};
                case NPTypeCode.Double: return new ArraySlice<Double>(UnmanagedMemoryBlock<Double>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDouble(CultureInfo.InvariantCulture)};
                case NPTypeCode.Single: return new ArraySlice<Single>(UnmanagedMemoryBlock<Single>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToSingle(CultureInfo.InvariantCulture)};
                case NPTypeCode.Decimal: return new ArraySlice<Decimal>(UnmanagedMemoryBlock<Decimal>.FromPool(_buffer)) {[0] = ((IConvertible)val).ToDecimal(CultureInfo.InvariantCulture)};
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Allocate(Type elementType, int count, object fill)
        {
            switch (elementType.GetTypeCode())
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#2>(new UnmanagedMemoryBlock<#2>(count, ((IConvertible)fill).To#1(CultureInfo.InvariantCulture)));
	            %
	            default:
		            throw new NotSupportedException();
#else
                case NPTypeCode.Boolean: return new ArraySlice<bool>(new UnmanagedMemoryBlock<bool>(count, ((IConvertible)fill).ToBoolean(CultureInfo.InvariantCulture)));
                case NPTypeCode.Byte: return new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>(count, ((IConvertible)fill).ToByte(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int16: return new ArraySlice<short>(new UnmanagedMemoryBlock<short>(count, ((IConvertible)fill).ToInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt16: return new ArraySlice<ushort>(new UnmanagedMemoryBlock<ushort>(count, ((IConvertible)fill).ToUInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int32: return new ArraySlice<int>(new UnmanagedMemoryBlock<int>(count, ((IConvertible)fill).ToInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt32: return new ArraySlice<uint>(new UnmanagedMemoryBlock<uint>(count, ((IConvertible)fill).ToUInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int64: return new ArraySlice<long>(new UnmanagedMemoryBlock<long>(count, ((IConvertible)fill).ToInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt64: return new ArraySlice<ulong>(new UnmanagedMemoryBlock<ulong>(count, ((IConvertible)fill).ToUInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.Char: return new ArraySlice<char>(new UnmanagedMemoryBlock<char>(count, ((IConvertible)fill).ToChar(CultureInfo.InvariantCulture)));
                case NPTypeCode.Double: return new ArraySlice<double>(new UnmanagedMemoryBlock<double>(count, ((IConvertible)fill).ToDouble(CultureInfo.InvariantCulture)));
                case NPTypeCode.Single: return new ArraySlice<float>(new UnmanagedMemoryBlock<float>(count, ((IConvertible)fill).ToSingle(CultureInfo.InvariantCulture)));
                case NPTypeCode.Decimal: return new ArraySlice<decimal>(new UnmanagedMemoryBlock<decimal>(count, ((IConvertible)fill).ToDecimal(CultureInfo.InvariantCulture)));
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : ArraySlice.cs
with Apache License 2.0
from SciSharp

public static IArraySlice Allocate(NPTypeCode typeCode, int count, object fill)
        {
            switch (typeCode)
            {
#if _REGEN
	            %foreach supported_dtypes,supported_dtypes_lowercase%
	            case NPTypeCode.#1: return new ArraySlice<#2>(new UnmanagedMemoryBlock<#2>(count, ((IConvertible)fill).To#1(CultureInfo.InvariantCulture)));
	            %
	            default:
		            throw new NotSupportedException();
#else
                case NPTypeCode.Boolean: return new ArraySlice<bool>(new UnmanagedMemoryBlock<bool>(count, ((IConvertible)fill).ToBoolean(CultureInfo.InvariantCulture)));
                case NPTypeCode.Byte: return new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>(count, ((IConvertible)fill).ToByte(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int16: return new ArraySlice<short>(new UnmanagedMemoryBlock<short>(count, ((IConvertible)fill).ToInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt16: return new ArraySlice<ushort>(new UnmanagedMemoryBlock<ushort>(count, ((IConvertible)fill).ToUInt16(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int32: return new ArraySlice<int>(new UnmanagedMemoryBlock<int>(count, ((IConvertible)fill).ToInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt32: return new ArraySlice<uint>(new UnmanagedMemoryBlock<uint>(count, ((IConvertible)fill).ToUInt32(CultureInfo.InvariantCulture)));
                case NPTypeCode.Int64: return new ArraySlice<long>(new UnmanagedMemoryBlock<long>(count, ((IConvertible)fill).ToInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.UInt64: return new ArraySlice<ulong>(new UnmanagedMemoryBlock<ulong>(count, ((IConvertible)fill).ToUInt64(CultureInfo.InvariantCulture)));
                case NPTypeCode.Char: return new ArraySlice<char>(new UnmanagedMemoryBlock<char>(count, ((IConvertible)fill).ToChar(CultureInfo.InvariantCulture)));
                case NPTypeCode.Double: return new ArraySlice<double>(new UnmanagedMemoryBlock<double>(count, ((IConvertible)fill).ToDouble(CultureInfo.InvariantCulture)));
                case NPTypeCode.Single: return new ArraySlice<float>(new UnmanagedMemoryBlock<float>(count, ((IConvertible)fill).ToSingle(CultureInfo.InvariantCulture)));
                case NPTypeCode.Decimal: return new ArraySlice<decimal>(new UnmanagedMemoryBlock<decimal>(count, ((IConvertible)fill).ToDecimal(CultureInfo.InvariantCulture)));
                default:
                    throw new NotSupportedException();
#endif
            }
        }

19 Source : Operator.cs
with Apache License 2.0
from SteeltoeOSS

public static bool EqualityCheck(IEvaluationContext context, object left, object right)
        {
            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return leftVal == rightVal;
                }
            }

            if (left is string && right is string)
            {
                return left.Equals(right);
            }

            if (left is bool && right is bool)
            {
                return left.Equals(right);
            }

            if (ObjectUtils.NullSafeEquals(left, right))
            {
                return true;
            }

            if (left is IComparable && right is IComparable)
            {
                var ancestor = ClreplacedUtils.DetermineCommonAncestor(left.GetType(), right.GetType());
                if (ancestor != null && typeof(IComparable).IsreplacedignableFrom(ancestor))
                {
                    return context.TypeComparator.Compare(left, right) == 0;
                }
            }

            return false;
        }

19 Source : OpGE.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) >= 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal >= rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) >= 0);
        }

19 Source : OpGT.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) > 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal > rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) > 0);
        }

19 Source : OpLE.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) <= 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal <= rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) <= 0);
        }

19 Source : OpMinus.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOp = LeftOperand;

            if (_children.Length < 2)
            {
                // if only one operand, then this is unary minus
                var operand = leftOp.GetValueInternal(state).Value;
                if (IsNumber(operand))
                {
                    switch (operand)
                    {
                        case decimal val: return new TypedValue(0M - val);
                        case double val:
                            _exitTypeDescriptor = TypeDescriptor.D;
                            return new TypedValue(0d - val);
                        case float val:
                            _exitTypeDescriptor = TypeDescriptor.F;
                            return new TypedValue(0f - val);
                        case long val:
                            _exitTypeDescriptor = TypeDescriptor.J;
                            return new TypedValue(0L - val);
                        case int val:
                            _exitTypeDescriptor = TypeDescriptor.I;
                            return new TypedValue(0 - val);
                        case short val: return new TypedValue(0 - val);
                        case byte val: return new TypedValue(0 - val);
                        case ulong val: return new TypedValue(0UL - val);
                        case uint val: return new TypedValue(0U - val);
                        case ushort val: return new TypedValue(0 - val);
                        case sbyte val: return new TypedValue(0 - val);
                        default: return state.Operate(Operation.SUBTRACT, operand, null);
                    }
                }

                return state.Operate(Operation.SUBTRACT, operand, null);
            }

            var left = leftOp.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            if (IsNumber(left) && IsNumber(right))
            {
                var leftNumber = (IConvertible)left;
                var rightNumber = (IConvertible)right;

                if (leftNumber is decimal || rightNumber is decimal)
                {
                    var leftVal = leftNumber.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDecimal(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal - rightVal);
                }
                else if (leftNumber is double || rightNumber is double)
                {
                    _exitTypeDescriptor = TypeDescriptor.D;
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal - rightVal);
                }
                else if (leftNumber is float || rightNumber is float)
                {
                    _exitTypeDescriptor = TypeDescriptor.F;
                    var leftVal = leftNumber.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToSingle(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal - rightVal);
                }
                else if (leftNumber is long || rightNumber is long)
                {
                    _exitTypeDescriptor = TypeDescriptor.J;
                    var leftVal = leftNumber.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt64(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal - rightVal);
                }
                else if (CodeFlow.IsIntegerForNumericOp(leftNumber) || CodeFlow.IsIntegerForNumericOp(rightNumber))
                {
                    _exitTypeDescriptor = TypeDescriptor.I;
                    var leftVal = leftNumber.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt32(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal - rightVal);
                }
                else
                {
                    // Unknown Number subtypes -> best guess is double subtraction
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal - rightVal);
                }
            }

            if (left is string str && right is int integer && str.Length == 1)
            {
                var theString = str;
                var theInteger = integer;

                // Implements character - int (ie. b - 1 = a)
                return new TypedValue(((char)(theString[0] - theInteger)).ToString());
            }

            return state.Operate(Operation.SUBTRACT, left, right);
        }

19 Source : OpPlus.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOp = LeftOperand;

            if (_children.Length < 2)
            {
                // if only one operand, then this is unary plus
                var operandOne = leftOp.GetValueInternal(state).Value;
                if (IsNumber(operandOne))
                {
                    if (operandOne is double)
                    {
                        _exitTypeDescriptor = TypeDescriptor.D;
                    }
                    else if (operandOne is float)
                    {
                        _exitTypeDescriptor = TypeDescriptor.F;
                    }
                    else if (operandOne is long)
                    {
                        _exitTypeDescriptor = TypeDescriptor.J;
                    }
                    else if (operandOne is int)
                    {
                        _exitTypeDescriptor = TypeDescriptor.I;
                    }

                    return new TypedValue(operandOne);
                }

                return state.Operate(Operation.ADD, operandOne, null);
            }

            var operandOneValue = leftOp.GetValueInternal(state);
            var leftOperand = operandOneValue.Value;
            var operandTwoValue = RightOperand.GetValueInternal(state);
            var rightOperand = operandTwoValue.Value;

            if (IsNumber(leftOperand) && IsNumber(rightOperand))
            {
                var leftNumber = (IConvertible)leftOperand;
                var rightNumber = (IConvertible)rightOperand;

                if (leftNumber is decimal || rightNumber is decimal)
                {
                    var leftVal = leftNumber.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDecimal(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal + rightVal);
                }
                else if (leftNumber is double || rightNumber is double)
                {
                    _exitTypeDescriptor = TypeDescriptor.D;
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal + rightVal);
                }
                else if (leftNumber is float || rightNumber is float)
                {
                    _exitTypeDescriptor = TypeDescriptor.F;
                    var leftVal = leftNumber.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToSingle(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal + rightVal);
                }
                else if (leftNumber is long || rightNumber is long)
                {
                    _exitTypeDescriptor = TypeDescriptor.J;
                    var leftVal = leftNumber.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt64(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal + rightVal);
                }
                else if (CodeFlow.IsIntegerForNumericOp(leftNumber) || CodeFlow.IsIntegerForNumericOp(rightNumber))
                {
                    _exitTypeDescriptor = TypeDescriptor.I;
                    var leftVal = leftNumber.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt32(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal + rightVal);
                }
                else
                {
                    // Unknown Number subtypes -> best guess is double addition
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal + rightVal);
                }
            }

            if (leftOperand is string strLeft && rightOperand is string strRight)
            {
                _exitTypeDescriptor = TypeDescriptor.STRING;
                return new TypedValue(strLeft + strRight);
            }

            if (leftOperand is string)
            {
                return new TypedValue(leftOperand + (rightOperand == null ? "null" : ConvertTypedValueToString(operandTwoValue, state)));
            }

            if (rightOperand is string)
            {
                return new TypedValue((leftOperand == null ? "null" : ConvertTypedValueToString(operandOneValue, state)) + rightOperand);
            }

            return state.Operate(Operation.ADD, leftOperand, rightOperand);
        }

19 Source : OpDivide.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOperand = LeftOperand.GetValueInternal(state).Value;
            var rightOperand = RightOperand.GetValueInternal(state).Value;

            if (IsNumber(leftOperand) && IsNumber(rightOperand))
            {
                var leftConv = (IConvertible)leftOperand;
                var rightConv = (IConvertible)rightOperand;

                if (leftOperand is decimal || rightOperand is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal / rightVal);
                }
                else if (leftOperand is double || rightOperand is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    _exitTypeDescriptor = TypeDescriptor.D;
                    return new TypedValue(leftVal / rightVal);
                }
                else if (leftOperand is float || rightOperand is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    _exitTypeDescriptor = TypeDescriptor.F;
                    return new TypedValue(leftVal / rightVal);
                }

                // Look at need to add support for .NET types not present in Java, e.g. ulong, ushort, byte, uint
                else if (leftOperand is long || rightOperand is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    _exitTypeDescriptor = TypeDescriptor.J;
                    return new TypedValue(leftVal / rightVal);
                }
                else if (CodeFlow.IsIntegerForNumericOp(leftOperand) || CodeFlow.IsIntegerForNumericOp(rightOperand))
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    _exitTypeDescriptor = TypeDescriptor.I;
                    return new TypedValue(leftVal / rightVal);
                }
                else
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);

                    // Unknown Number subtypes -> best guess is double division
                    // Look at need to add support for .NET types not present in Java, e.g. ulong, ushort, byte, uint
                    return new TypedValue(leftVal / rightVal);
                }
            }

            return state.Operate(Operation.DIVIDE, leftOperand, rightOperand);
        }

19 Source : OpLT.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var left = LeftOperand.GetValueInternal(state).Value;
            var right = RightOperand.GetValueInternal(state).Value;

            _leftActualDescriptor = CodeFlow.ToDescriptorFromObject(left);
            _rightActualDescriptor = CodeFlow.ToDescriptorFromObject(right);

            if (IsNumber(left) && IsNumber(right))
            {
                var leftConv = (IConvertible)left;
                var rightConv = (IConvertible)right;

                if (left is decimal || right is decimal)
                {
                    var leftVal = leftConv.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDecimal(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal.CompareTo(rightVal) < 0);
                }
                else if (left is double || right is double)
                {
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is float || right is float)
                {
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is long || right is long)
                {
                    var leftVal = leftConv.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is int || right is int)
                {
                    var leftVal = leftConv.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is short || right is short)
                {
                    var leftVal = leftConv.ToInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is byte || right is byte)
                {
                    var leftVal = leftConv.ToByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is ulong || right is ulong)
                {
                    var leftVal = leftConv.ToUInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt64(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is uint || right is uint)
                {
                    var leftVal = leftConv.ToUInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt32(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is ushort || right is ushort)
                {
                    var leftVal = leftConv.ToUInt16(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToUInt16(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
                else if (left is sbyte || right is sbyte)
                {
                    var leftVal = leftConv.ToSByte(CultureInfo.InvariantCulture);
                    var rightVal = rightConv.ToSByte(CultureInfo.InvariantCulture);
                    return BooleanTypedValue.ForValue(leftVal < rightVal);
                }
            }

            return BooleanTypedValue.ForValue(state.TypeComparator.Compare(left, right) < 0);
        }

19 Source : OpMultiply.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOperand = LeftOperand.GetValueInternal(state).Value;
            var rightOperand = RightOperand.GetValueInternal(state).Value;

            if (IsNumber(leftOperand) && IsNumber(rightOperand))
            {
                var leftNumber = (IConvertible)leftOperand;
                var rightNumber = (IConvertible)rightOperand;

                if (leftNumber is decimal || rightNumber is decimal)
                {
                    var leftVal = leftNumber.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDecimal(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal * rightVal);
                }
                else if (leftNumber is double || rightNumber is double)
                {
                    _exitTypeDescriptor = TypeDescriptor.D;
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal * rightVal);
                }
                else if (leftNumber is float || rightNumber is float)
                {
                    _exitTypeDescriptor = TypeDescriptor.F;
                    var leftVal = leftNumber.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToSingle(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal * rightVal);
                }
                else if (leftNumber is long || rightNumber is long)
                {
                    _exitTypeDescriptor = TypeDescriptor.J;
                    var leftVal = leftNumber.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt64(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal * rightVal);
                }
                else if (CodeFlow.IsIntegerForNumericOp(leftNumber) || CodeFlow.IsIntegerForNumericOp(rightNumber))
                {
                    _exitTypeDescriptor = TypeDescriptor.I;
                    var leftVal = leftNumber.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt32(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal * rightVal);
                }
                else
                {
                    // Unknown Number subtypes -> best guess is double multiplication
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal * rightVal);
                }
            }

            if (leftOperand is string && rightOperand is int integer)
            {
                var repeats = integer;
                var result = new StringBuilder();
                for (var i = 0; i < repeats; i++)
                {
                    result.Append(leftOperand);
                }

                return new TypedValue(result.ToString());
            }

            return state.Operate(Operation.MULTIPLY, leftOperand, rightOperand);
        }

19 Source : OperatorPower.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOp = LeftOperand;
            var rightOp = RightOperand;

            var leftOperand = leftOp.GetValueInternal(state).Value;
            var rightOperand = rightOp.GetValueInternal(state).Value;

            if (IsNumber(leftOperand) && IsNumber(rightOperand))
            {
                var leftConv = (IConvertible)leftOperand;
                var rightConv = (IConvertible)rightOperand;

                if (leftOperand is decimal)
                {
                    var rightVal = rightConv.ToInt32(CultureInfo.InvariantCulture);
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    var result = Math.Pow(leftVal, rightVal) as IConvertible;
                    return new TypedValue(result.ToDecimal(CultureInfo.InvariantCulture));
                }
                else if (leftOperand is double || rightOperand is double)
                {
                    var rightVal = rightConv.ToDouble(CultureInfo.InvariantCulture);
                    var leftVal = leftConv.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(Math.Pow(leftVal, rightVal));
                }
                else if (leftOperand is float || rightOperand is float)
                {
                    var rightVal = rightConv.ToSingle(CultureInfo.InvariantCulture);
                    var leftVal = leftConv.ToSingle(CultureInfo.InvariantCulture);
                    return new TypedValue(Math.Pow(leftVal, rightVal));
                }

                var r = rightConv.ToDouble(CultureInfo.InvariantCulture);
                var l = leftConv.ToDouble(CultureInfo.InvariantCulture);

                var d = Math.Pow(l, r);
                var asLong = (long)d;
                if (asLong > int.MaxValue || leftOperand is long || rightOperand is long)
                {
                    return new TypedValue(asLong);
                }
                else
                {
                    return new TypedValue((int)asLong);
                }
            }

            return state.Operate(Operation.POWER, leftOperand, rightOperand);
        }

19 Source : OpModulus.cs
with Apache License 2.0
from SteeltoeOSS

public override ITypedValue GetValueInternal(ExpressionState state)
        {
            var leftOperand = LeftOperand.GetValueInternal(state).Value;
            var rightOperand = RightOperand.GetValueInternal(state).Value;

            if (IsNumber(leftOperand) && IsNumber(rightOperand))
            {
                var leftNumber = (IConvertible)leftOperand;
                var rightNumber = (IConvertible)rightOperand;

                if (leftNumber is decimal || rightNumber is decimal)
                {
                    var leftVal = leftNumber.ToDecimal(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDecimal(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal % rightVal);
                }
                else if (leftNumber is double || rightNumber is double)
                {
                    _exitTypeDescriptor = TypeDescriptor.D;
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal % rightVal);
                }
                else if (leftNumber is float || rightNumber is float)
                {
                    _exitTypeDescriptor = TypeDescriptor.F;
                    var leftVal = leftNumber.ToSingle(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToSingle(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal % rightVal);
                }
                else if (leftNumber is long || rightNumber is long)
                {
                    _exitTypeDescriptor = TypeDescriptor.J;
                    var leftVal = leftNumber.ToInt64(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt64(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal % rightVal);
                }
                else if (CodeFlow.IsIntegerForNumericOp(leftNumber) || CodeFlow.IsIntegerForNumericOp(rightNumber))
                {
                    _exitTypeDescriptor = TypeDescriptor.I;
                    var leftVal = leftNumber.ToInt32(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToInt32(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal % rightVal);
                }
                else
                {
                    // Unknown Number subtypes -> best guess is double division
                    var leftVal = leftNumber.ToDouble(CultureInfo.InvariantCulture);
                    var rightVal = rightNumber.ToDouble(CultureInfo.InvariantCulture);
                    return new TypedValue(leftVal % rightVal);
                }
            }

            return state.Operate(Operation.MODULUS, leftOperand, rightOperand);
        }

19 Source : Convert.cs
with Apache License 2.0
from theolivenbaum

internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
        {
            Debug.replacedert(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (ReferenceEquals(value.GetType(), targetType))
            {
                return value;
            }

            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Boolean]))
                return value.ToBoolean(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Char]))
                return value.ToChar(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.SByte]))
                return value.ToSByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Byte]))
                return value.ToByte(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int16]))
                return value.ToInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt16]))
                return value.ToUInt16(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int32]))
                return value.ToInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt32]))
                return value.ToUInt32(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int64]))
                return value.ToInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt64]))
                return value.ToUInt64(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Single]))
                return value.ToSingle(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Double]))
                return value.ToDouble(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Decimal]))
                return value.ToDecimal(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DateTime]))
                return value.ToDateTime(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.String]))
                return value.ToString(provider);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Object]))
                return (object)value;
            //  Need to special case Enum because typecode will be underlying type, e.g. Int32
            if (ReferenceEquals(targetType, EnumType))
                return (Enum)value;
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DBNull]))
                throw new InvalidCastException("Object cannot be cast to DBNull.");
            // TODO: SR
            //throw new InvalidCastException(SR.InvalidCast_DBNull);
            if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Empty]))
                throw new InvalidCastException("Object cannot be cast to Empty.");
            // TODO: SR
            //throw new InvalidCastException(SR.InvalidCast_Empty);

            throw new InvalidCastException(string.Format("Invalid cast from '{0}' to '{1}'.", value.GetType().FullName, targetType.FullName));
            // TODO: SR
            //throw new InvalidCastException(string.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullName));
        }

19 Source : NumberSerializer.cs
with Apache License 2.0
from tonyredondo

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Write(BinaryWriter writer, object value)
        {
            var valueType = value?.GetType();
            var decType = DataTypeHelper.GetDecreaseDataType(value, valueType);
            var convValue = (IConvertible) value ?? 0;
            int objIdx;
            switch (decType)
            {
                case DataType.Decimal:
                    #region Decimal Type
                    var v1 = convValue.ToDecimal(null);
                    if (v1 == default(decimal))
                    {
                        writer.Write(DataType.DecimalDefault);
                        return;
                    }
                    objIdx = DecimalCache.SerializerGet(v1);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefDecimalByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefDecimalUShort, (ushort)objIdx);
                    }
                    else
                    {
                        writer.Write(DataType.Decimal);
                        writer.Write(v1);
                        DecimalCache.SerializerSet(v1);
                    }
                    #endregion
                    return;
                case DataType.Double:
                    #region Double Type
                    var v2 = convValue.ToDouble(null);
                    if (Math.Abs(v2 - default(double)) < 0.0000000000001)
                    {
                        writer.Write(DataType.DoubleDefault);
                        return;
                    }
                    objIdx = DoubleCache.SerializerGet(v2);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefDoubleByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefDoubleUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteDouble(writer, DataType.Double, v2);
                        DoubleCache.SerializerSet(v2);
                    }
                    #endregion
                    return;
                case DataType.Float:
                    #region Float Type
                    var v3 = convValue.ToSingle(null);
                    if (Math.Abs(v3 - default(float)) < 0.0000000000001)
                    {
                        writer.Write(DataType.FloatDefault);
                        return;
                    }
                    objIdx = FloatCache.SerializerGet(v3);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefFloatByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefFloatUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteFloat(writer, DataType.Float, v3);
                        FloatCache.SerializerSet(v3);
                    }
                    #endregion
                    return;
                case DataType.Long:
                    #region Long Type
                    var v4 = convValue.ToInt64(null);
                    objIdx = LongCache.SerializerGet(v4);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefLongByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefLongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteLong(writer, DataType.Long, v4);
                        LongCache.SerializerSet(v4);
                    }
                    #endregion
                    return;
                case DataType.ULong:
                    #region ULong Type
                    var v5 = convValue.ToUInt64(null);
                    objIdx = ULongCache.SerializerGet(v5);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefULongByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefULongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteULong(writer, DataType.ULong, v5);
                        ULongCache.SerializerSet(v5);
                    }
                    #endregion
                    return;
                case DataType.Int:
                    #region Int Type
                    var v6 = convValue.ToInt32(null);
                    objIdx = IntCache.SerializerGet(v6);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefIntByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteInt(writer, DataType.Int, v6);
                        IntCache.SerializerSet(v6);
                    }
                    #endregion
                    return;
                case DataType.UInt:
                    #region UInt Type
                    var v7 = convValue.ToUInt32(null);
                    objIdx = UIntCache.SerializerGet(v7);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteHelper.WriteByte(writer, DataType.RefUIntByte, (byte)objIdx);
                        else
                            WriteHelper.WriteUshort(writer, DataType.RefUIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteHelper.WriteUInt(writer, DataType.UInt, v7);
                        UIntCache.SerializerSet(v7);
                    }
                    #endregion
                    return;
                case DataType.Short:
                    #region Short Type
                    var v8 = convValue.ToInt16(null);
                    objIdx = ShortCache.SerializerGet(v8);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteHelper.WriteByte(writer, DataType.RefShortByte, (byte)objIdx);
                    else
                    {
                        WriteHelper.WriteShort(writer, DataType.Short, v8);
                        ShortCache.SerializerSet(v8);
                    }
                    #endregion
                    return;
                case DataType.UShort:
                    #region UShort Type
                    var v9 = convValue.ToUInt16(null);
                    objIdx = UShortCache.SerializerGet(v9);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteHelper.WriteByte(writer, DataType.RefUShortByte, (byte)objIdx);
                    else
                    {
                        WriteHelper.WriteUshort(writer, DataType.UShort, v9);
                        UShortCache.SerializerSet(v9);
                    }
                    #endregion
                    return;
                case DataType.Byte:
                    #region Byte Type
                    var v10 = convValue.ToByte(null);
                    switch (v10)
                    {
                        case 0:
                            writer.Write(DataType.ByteDefault);
                            return;
                        case 1:
                            writer.Write(DataType.Byte1);
                            return;
                        case 2:
                            writer.Write(DataType.Byte2);
                            return;
                        case 3:
                            writer.Write(DataType.Byte3);
                            return;
                        case 4:
                            writer.Write(DataType.Byte4);
                            return;
                        case 5:
                            writer.Write(DataType.Byte5);
                            return;
                        case 6:
                            writer.Write(DataType.Byte6);
                            return;
                        case 7:
                            writer.Write(DataType.Byte7);
                            return;
                        case 8:
                            writer.Write(DataType.Byte8);
                            return;
                        case 9:
                            writer.Write(DataType.Byte9);
                            return;
                        case 10:
                            writer.Write(DataType.Byte10);
                            return;
                        case 11:
                            writer.Write(DataType.Byte11);
                            return;
                        case 12:
                            writer.Write(DataType.Byte12);
                            return;
                        case 13:
                            writer.Write(DataType.Byte13);
                            return;
                        case 14:
                            writer.Write(DataType.Byte14);
                            return;
                        case 15:
                            writer.Write(DataType.Byte15);
                            return;
                        case 16:
                            writer.Write(DataType.Byte16);
                            return;
                        case 17:
                            writer.Write(DataType.Byte17);
                            return;
                        case 18:
                            writer.Write(DataType.Byte18);
                            return;
                        case 19:
                            writer.Write(DataType.Byte19);
                            return;
                        case 20:
                            writer.Write(DataType.Byte20);
                            return;
                        default:
                            WriteHelper.WriteByte(writer, DataType.Byte, v10);
                            return;
                    }
                #endregion
                case DataType.SByte:
                    #region SByte Type
                    var sByte = convValue.ToSByte(null);
                    switch (sByte)
                    {
                        case -1:
                            writer.Write(DataType.SByteMinusOne);
                            return;
                        default:
                            writer.Write(DataType.SByte);
                            writer.Write(sByte);
                            return;
                    }
                    #endregion
            }
        }

19 Source : NumberSerializer.cs
with Apache License 2.0
from tonyredondo

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override void Write(BinaryWriter writer, object value)
        {
            var valueType = value?.GetType();
            var decType = DataTypeHelper.GetDecreaseDataType(value, valueType);
            var convValue = (IConvertible) value ?? 0;
            int objIdx;
            switch (decType)
            {
                case DataType.Decimal:
                    #region Decimal Type
                    var v1 = convValue.ToDecimal(null);
                    if (v1 == default(decimal))
                    {
                        writer.Write(DataType.DecimalDefault);
                        return;
                    }
                    objIdx = DecimalCache.SerializerGet(v1);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefDecimalByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefDecimalUShort, (ushort)objIdx);
                    }
                    else
                    {
                        writer.Write(DataType.Decimal);
                        writer.Write(v1);
                        DecimalCache.SerializerSet(v1);
                    }
                    #endregion
                    return;
                case DataType.Double:
                    #region Double Type
                    var v2 = convValue.ToDouble(null);
                    if (Math.Abs(v2 - default(double)) < 0.0000000000001)
                    {
                        writer.Write(DataType.DoubleDefault);
                        return;
                    }
                    objIdx = DoubleCache.SerializerGet(v2);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefDoubleByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefDoubleUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteDouble(writer, DataType.Double, v2);
                        DoubleCache.SerializerSet(v2);
                    }
                    #endregion
                    return;
                case DataType.Float:
                    #region Float Type
                    var v3 = convValue.ToSingle(null);
                    if (Math.Abs(v3 - default(float)) < 0.0000000000001)
                    {
                        writer.Write(DataType.FloatDefault);
                        return;
                    }
                    objIdx = FloatCache.SerializerGet(v3);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefFloatByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefFloatUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteFloat(writer, DataType.Float, v3);
                        FloatCache.SerializerSet(v3);
                    }
                    #endregion
                    return;
                case DataType.Long:
                    #region Long Type
                    var v4 = convValue.ToInt64(null);
                    objIdx = LongCache.SerializerGet(v4);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefLongByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefLongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteLong(writer, DataType.Long, v4);
                        LongCache.SerializerSet(v4);
                    }
                    #endregion
                    return;
                case DataType.ULong:
                    #region ULong Type
                    var v5 = convValue.ToUInt64(null);
                    objIdx = ULongCache.SerializerGet(v5);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefULongByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefULongUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteULong(writer, DataType.ULong, v5);
                        ULongCache.SerializerSet(v5);
                    }
                    #endregion
                    return;
                case DataType.Int:
                    #region Int Type
                    var v6 = convValue.ToInt32(null);
                    objIdx = IntCache.SerializerGet(v6);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefIntByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteInt(writer, DataType.Int, v6);
                        IntCache.SerializerSet(v6);
                    }
                    #endregion
                    return;
                case DataType.UInt:
                    #region UInt Type
                    var v7 = convValue.ToUInt32(null);
                    objIdx = UIntCache.SerializerGet(v7);
                    if (objIdx > -1)
                    {
                        if (objIdx <= byte.MaxValue)
                            WriteByte(writer, DataType.RefUIntByte, (byte)objIdx);
                        else
                            WriteUshort(writer, DataType.RefUIntUShort, (ushort)objIdx);
                    }
                    else
                    {
                        WriteUInt(writer, DataType.UInt, v7);
                        UIntCache.SerializerSet(v7);
                    }
                    #endregion
                    return;
                case DataType.Short:
                    #region Short Type
                    var v8 = convValue.ToInt16(null);
                    objIdx = ShortCache.SerializerGet(v8);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteByte(writer, DataType.RefShortByte, (byte)objIdx);
                    else
                    {
                        WriteShort(writer, DataType.Short, v8);
                        ShortCache.SerializerSet(v8);
                    }
                    #endregion
                    return;
                case DataType.UShort:
                    #region UShort Type
                    var v9 = convValue.ToUInt16(null);
                    objIdx = UShortCache.SerializerGet(v9);
                    if (objIdx > -1 && objIdx <= byte.MaxValue)
                        WriteByte(writer, DataType.RefUShortByte, (byte)objIdx);
                    else
                    {
                        WriteUshort(writer, DataType.UShort, v9);
                        UShortCache.SerializerSet(v9);
                    }
                    #endregion
                    return;
                case DataType.Byte:
                    #region Byte Type
                    var v10 = convValue.ToByte(null);
                    switch (v10)
                    {
                        case 0:
                            writer.Write(DataType.ByteDefault);
                            return;
                        case 1:
                            writer.Write(DataType.Byte1);
                            return;
                        case 2:
                            writer.Write(DataType.Byte2);
                            return;
                        case 3:
                            writer.Write(DataType.Byte3);
                            return;
                        case 4:
                            writer.Write(DataType.Byte4);
                            return;
                        case 5:
                            writer.Write(DataType.Byte5);
                            return;
                        case 6:
                            writer.Write(DataType.Byte6);
                            return;
                        case 7:
                            writer.Write(DataType.Byte7);
                            return;
                        case 8:
                            writer.Write(DataType.Byte8);
                            return;
                        case 9:
                            writer.Write(DataType.Byte9);
                            return;
                        case 10:
                            writer.Write(DataType.Byte10);
                            return;
                        case 11:
                            writer.Write(DataType.Byte11);
                            return;
                        case 12:
                            writer.Write(DataType.Byte12);
                            return;
                        case 13:
                            writer.Write(DataType.Byte13);
                            return;
                        case 14:
                            writer.Write(DataType.Byte14);
                            return;
                        case 15:
                            writer.Write(DataType.Byte15);
                            return;
                        case 16:
                            writer.Write(DataType.Byte16);
                            return;
                        default:
                            WriteByte(writer, DataType.Byte, v10);
                            return;
                    }
                #endregion
                case DataType.SByte:
                    #region SByte Type
                    var sByte = convValue.ToSByte(null);
                    switch (sByte)
                    {
                        case -1:
                            writer.Write(DataType.SByteMinusOne);
                            return;
                        default:
                            writer.Write(DataType.SByte);
                            writer.Write(sByte);
                            return;
                    }
                    #endregion
            }
        }

19 Source : TwentyFourBitIntegers.cs
with GNU General Public License v3.0
from tyranid

decimal IConvertible.ToDecimal(IFormatProvider provider)
        {
            IConvertible conv = (IConvertible)value;

            return conv.ToDecimal(provider);
        }

19 Source : JsonContent.Impl.cs
with MIT License
from vpenades

private static bool AreEqual(IConvertible x, IConvertible y, float precission)
        {
            var xc = x.GetTypeCode();
            var yc = y.GetTypeCode();

            if (xc == TypeCode.Decimal || yc == TypeCode.Decimal)
            {
                var xf = y.ToDecimal(CULTURE.InvariantCulture);
                var yf = y.ToDecimal(CULTURE.InvariantCulture);
                return Math.Abs((double)(xf - yf)) < precission;
            }

            if (xc == TypeCode.Double || yc == TypeCode.Double)
            {
                var xf = y.ToDouble(CULTURE.InvariantCulture);
                var yf = y.ToDouble(CULTURE.InvariantCulture);
                return Math.Abs(xf - yf) < precission;
            }

            if (xc == TypeCode.Single || yc == TypeCode.Single)
            {
                var xf = y.ToSingle(CULTURE.InvariantCulture);
                var yf = y.ToSingle(CULTURE.InvariantCulture);
                return Math.Abs(xf - yf) < precission;
            }

            if (xc == yc) return Object.Equals(x, y);

            return false;
        }

19 Source : NIntNUInt.cs
with MIT License
from xamarin

decimal IConvertible.ToDecimal (IFormatProvider provider) { return ((IConvertible)v).ToDecimal (provider); }

19 Source : KalkBool.cs
with BSD 2-Clause "Simplified" License
from xoofx

decimal IConvertible.ToDecimal(IFormatProvider? provider)
        {
            return ((IConvertible) _value).ToDecimal(provider);
        }

19 Source : KTIDReference.cs
with MIT License
from yretenai

public decimal ToDecimal(IFormatProvider? provider) => ((IConvertible) KTID).ToDecimal(provider);