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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s