Exemple #1
0
        /// <summary>Returns a <see langword="Single" /> value that corresponds to the specified object. </summary>
        /// <param name="Value">Required. Object to convert to a <see langword="Single" /> 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="Single" /> value corresponding to <paramref name="Value" />.</returns>
        public static float FromObject(object Value, NumberFormatInfo NumberFormat)
        {
            if (Value == null)
            {
                return(0.0f);
            }
            IConvertible ValueInterface = Value as IConvertible;

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

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

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

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

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

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

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

                case TypeCode.Decimal:
                    return(SingleType.DecimalToSingle(ValueInterface));

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