Not really, the number of decimal places is a function of the internal representation of the number. What you see when you display the number is, is the number as a string for printing.
So if you are interested in the displayed value, you can control the number of decimal places with formatting directives in the first place e.g., %5.2f
, or use the string function approach as you mention in your post after the fact once you have the number as a string.
Also, as an aside, would you count trailing zeros?
Perhaps it would be helpful to state your goal for wanting to do this? There might be other ways to accomplish what you are looking to do.
Update:
Based on your update, the problem is really not counting decimal places, rather you are running into represenation issue/rounding errors (not that uncommon). The problem is that some fractional values can't be exactly represented in base 2. Here's an exhaustive explanation: What Every Computer Scientist Should Know About Floating-Point Arithmetic.
Take a look at this SO question: Understanding floating point representation errors; what's wrong with my thinking?
You can try this approach to round the value:
float rounded_val = floorf(value * 100.0 + 0.5) / 100.0;
Wikipeadia has a whole article on rounding.