I would like a c# function with this signature:
int GetSignificantNumberOfDecimalPlaces(decimal d)
It should behave as follows when called:
GetSignificantNumberOfDecimalPlaces(2.12300m); // returns 3
GetSignificantNumberOfDecimalPlaces(2.123m); // returns 3
GetSignificantNumberOfDecimalPlaces(2.103450m); // returns 5
GetSignificantNumberOfDecimalPlaces(2.0m); // returns 0
GetSignificantNumberOfDecimalPlaces(2.00m); // returns 0
GetSignificantNumberOfDecimalPlaces(2m); // returns 0
i.e. for a given decimal, i want the number of significant decimal places to the right of the decimal point. So trailing zeros can be ignored. My fallback is to turn the decimal into a string, trim trailing zeros, and get the length this way. But is there a better way ?
NOTE: I may be using the word "significant" incorrectly here. The required return values in the example should hopefully explain what i'm after.