示例#1
0
        /// <summary>Returns a <see langword="Double" /> value that corresponds to the specified object. </summary>
        /// <param name="Value">Required. Object to convert to a <see langword="Double" /> value.</param>
        /// <param name="NumberFormat">A <see cref="T:System.Globalization.NumberFormatInfo" /> object that defines how numeric values are formatted and displayed, depending on the culture.</param>
        /// <returns>The <see langword="Double" /> value corresponding to <paramref name="Value" />.</returns>
        public static double FromObject(object Value, NumberFormatInfo NumberFormat)
        {
            if (Value == null)
            {
                return(0.0);
            }
            IConvertible ValueInterface = Value as IConvertible;

            if (ValueInterface != null)
            {
                switch (ValueInterface.GetTypeCode())
                {
                case TypeCode.Boolean:
                    return((double)-(ValueInterface.ToBoolean((IFormatProvider)null) ? 1 : 0));

                case TypeCode.Byte:
                    if (Value is byte)
                    {
                        return((double)(byte)Value);
                    }
                    return((double)ValueInterface.ToByte((IFormatProvider)null));

                case TypeCode.Int16:
                    if (Value is short)
                    {
                        return((double)(short)Value);
                    }
                    return((double)ValueInterface.ToInt16((IFormatProvider)null));

                case TypeCode.Int32:
                    if (Value is int)
                    {
                        return((double)(int)Value);
                    }
                    return((double)ValueInterface.ToInt32((IFormatProvider)null));

                case TypeCode.Int64:
                    if (Value is long)
                    {
                        return((double)(long)Value);
                    }
                    return((double)ValueInterface.ToInt64((IFormatProvider)null));

                case TypeCode.Single:
                    if (Value is float)
                    {
                        return((double)(float)Value);
                    }
                    return((double)ValueInterface.ToSingle((IFormatProvider)null));

                case TypeCode.Double:
                    if (Value is double)
                    {
                        return((double)Value);
                    }
                    return(ValueInterface.ToDouble((IFormatProvider)null));

                case TypeCode.Decimal:
                    return(DoubleType.DecimalToDouble(ValueInterface));

                case TypeCode.String:
                    return(DoubleType.FromString(ValueInterface.ToString((IFormatProvider)null), NumberFormat));
                }
            }
            throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Double"));
        }
示例#2
0
 /// <summary>Returns a <see langword="Double" /> value that corresponds to the specified string. </summary>
 /// <param name="Value">Required. String to convert to a <see langword="Double" /> value.</param>
 /// <returns>The <see langword="Double" /> value corresponding to <paramref name="Value" />.</returns>
 public static double FromString(string Value)
 {
     return(DoubleType.FromString(Value, (NumberFormatInfo)null));
 }