Thursday, July 30, 2015

Convert - - StringToInt / intTostring / doubleTostring / NullToString /ConvertToDouble / NullToString

 Convert - - StringToInt / intTostring / doubleTostring / NullToString /ConvertToDouble / NullToString


   /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="instance">The instance.</param>
        /// <returns>T.</returns>

        public static T GetValue<T>(this string instance)
        {
            T obj = default(T);
            try
            {
                obj = GenericDataConverter<T>.Convert(instance);
                return obj;
            }
            catch
            {
                throw;
            }
        }
----------------------------------------------------
        /// <summary>
        /// Converts the string to int.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.Int32.</returns>

        public static int ConvertStringToInt(this string value)
        {
            int retValue = 0;
            try
            {
                if (value != "" && value != null)
                {
                    retValue = Convert.ToInt32(value);
                }
                else
                {
                    retValue = 0;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }
        /// <summary>
        /// Converts the int to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertIntToString(this int value)
        {
            string retValue = string.Empty;
            try
            {
                if (value >= 0)
                {
                    retValue = Convert.ToString(value);
                }
                else
                {
                    retValue = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }
 ---------------------------------------------------------------------------------------
        /// <summary>
        /// Converts the double to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertDoubleToString(this double value)
        {
            string retValue = string.Empty;
            try
            {
                if (value >= 0 || value < 0)
                {
                    retValue = string.Format("{0:0.00}", value);
                    //retValue = value.ToString("0.##");
                }
                else
                {
                    retValue = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }

------------------------------------------------------------------------
        /// <summary>
        /// Converts the null to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertNullToString(this string value)
        {
            try
            {
                if (string.IsNullOrEmpty(value))
                {
                    value = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return value;
        }

----------------------------------------------------------------------
        /// <summary>
        /// Converts to double.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.Double.</returns>
        public static double ConvertToDouble(this object value)
        {
            double retValue = 0;
            try
            {
                if (value.ConvertNullToString() != "")
                {
                    retValue = Convert.ToDouble(value);
                }
                else
                {
                    retValue = 0;
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }

------------------------------------------------------------
        /// <summary>
        /// Converts the null to string.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertNullToString(this object value)
        {
            try
            {
                if (value == null)
                {
                    value = string.Empty;
                }
            }
            catch
            {
                throw;
            }
            return value.ToString();
        }



----------------------------------------------------------------
        /// <summary>
        /// Converts to currency.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>System.String.</returns>
        public static string ConvertToCurrency(this object value)
        {
            //ToString("C", CultureInfo.CurrentCulture)
            string retValue = "$0.00";
            try
            {
                if (value.ConvertNullToString() != "")
                {
                    CultureInfo currentCulture = CultureInfo.CurrentCulture;
                    CultureInfo newCulture = new CultureInfo(currentCulture.Name);
                    newCulture.NumberFormat.CurrencyNegativePattern = 1;
                    retValue = value.ConvertToDouble().ToString("C", newCulture);
                }
                else
                {
                    retValue = "$0.00";
                }
            }
            catch
            {
                throw;
            }
            return retValue;
        }

No comments:

Post a Comment