In C#, I have a situation where I have two possible numbers in a textbox control.
The numbers can be either:
a) .xxxx
or
b) .xx
How do I write a condition that says, "If the textbox has 4 decimal places, then call this function, otherwise, if the textbox has 2 decimal places, then call this function."
Seems easy, but I don't know how to evaluate the decimal places.
Thanks much!
Condition will evaluatetrue
on two but not four decimal places:EDIT: above condition works only for Decimal type. Well, following works for real numbers of all types:
EDIT: Well, if you are interested in strings then use following (extension string contains last point and subsequent digits/numbers):
You could take advantage of a very obscure feature of the Decimal type. Its internal representation is a 96-bit number with an exponent. The exponent is equal to the number of digits in the fraction, even if the fractional digits are zero. Thus:
Use decimal.TryParse() if you need to validate the user input.
You can use Regex
you may want to take the decimal separator from the system's current culture instead of hardcoding the dot.
This may not work perfectly depending on your needs, but it works for my purposes and could be useful to someone else.