Monday, January 30, 2012

Best Extension Methods: Convert To Double

Below extension method converts a value/object to double and if its not convertible then returns 0.0;
//use it like: string s=4.5; double d = s.ToDouble();
public static double ToDouble(this object source)
    {
        double val = 0;
        if (source != null)
        {
            string str = Convert.ToString(source);
            if (! string.IsNullOrEmpty(str))
            {
                double.TryParse(str, out val);

            }
        }
        return val;
    }

Compiled By: Rajesh Rolen

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates