C# Tips n Tricks: Get Decimal Places

I needed a quick way to get the number of decimal places on a Double value.  I am displaying the values differently if they have more than 2 decimal values.  Here is a quick helper that I threw together.

public static int GetDecimalCount(this double value)
{
    var str = value.ToStringOrEmpty();
    if (!str.Contains("."))
        return 0;
    return str.Substring(str.IndexOf(".") + 1).Length;
}

Leave a comment