If you know you're not going to have parsing issues (or if you're letting python itself or some other library handle that for you, hopefully handling localization issues)... just parse it and use modf. The return value is a pair of values, one of which is the integral part, the other is the fractional part.
"the number of decimal places" is not a property a floating point number has, because of the way they are stored and handled internally.
You can get as many decimal places as you like from a floating point number. The question is how much accuracy you want. When converting a floating point number to a string, part of the process is deciding on the accuracy.
Try for instance:
1.1 - int(1.1)
And you will see that the answer is:
0.10000000000000009
So, for this case, the number of decimals is 17. This is probably not the number you are looking for.
You can, however, round the number to a certain number of decimals with "round":
round(3.1415 - int(3.1415), 3)
For this case, the number of decimals is cut to 3.
You can't get "the number of decimals from a float", but you can decide the accuracy and how many you want.
Converting a float to a string is one way of making such a decision.
Since Python floating point numbers are internally represented as binary rather than decimal, there's really no shortcut other than converting to decimal. The only built-in way to do that is by converting to a string. You could write your own code to do a decimal conversion and count the digits, but it would be a duplication of effort.
To repeat what others have said (because I had already typed it out!), I'm not even sure such a value would be meaningful in the case of a floating point number, because of the difference between the decimal and binary representation; often a number representable by a finite number of decimal digits will have only an infinite-digit representation in binary.
In the case of a decimal.Decimal object, you can retrieve the exponent using the as_tuple method, which returns a namedtuple with sign, digits, and exponent attributes:
>>> d = decimal.Decimal('56.4325')
>>> d.as_tuple().exponent
-4
>>> d = decimal.Decimal('56.43256436')
>>> d.as_tuple().exponent
-8
The negation of the exponent is the number of digits after the decimal point, unless the exponent is greater than 0.
The decimal library is for working with decimal numbers, like in Accounting. It doesn't inherently have a function to return the number of decimal places. This is especially a problem when you realize that the context it runs under sets it at whatever the user wants.
If you get a string, you can convert to decimal, but this will either tack on zeros to get you to your accuracy, or use the rounding setting to truncate it.
Your best bet would probably bet splitting on the dot in your string and counting the number of chars in the resulting substring.
I needed something like this and i tested some of these, the fastest i found out was :
str(number)[::-1].find('.')
Because of the floating point issue all the modulo ones gave me false results even with Decimal(number) (note that i needed this in a script to fix prices of an entire db)
len(str(Decimal(str(number))) % Decimal(1))) - 2
The need to put a string into Decimal is quite uneasy when we have floats or something like.
This makes use of the scientific notation (m x 10^n), which already provides the number of decimal places in form of the power (n) of 10, to which the coefficient (m) is raised, ie. -3, derived from 1.000000e-03 for the above example.
An important difference to Decimal(str(fraction)).as_tuple().exponent is that the scientific notation will always only consider the most significant digit for the exponent n, similar to Decimal().adjusted(), whereas Decimal().as_tuple().exponent returns the exponent of the least significant digit.
String formatting to scientific notation can of course also only handle floats and no strings, thus any floating point arithmetic issues persist. However, for many use cases the above might be sufficient.
The way it works is to convert the input decimal to characters, when it detects ".", it will add the following decimal to decimals, and finally return its length
def decimal(obj):
is_point = False
decimals = []
for get_float in str(obj):
if is_point:
decimals.append(get_float)
if get_float == ".":
is_point = True
return len(decimals)