Easy way of finding decimal places

Is there an easy way or integrated function to find out the decimal places of a floating point number?

The number is parsed from a string, so one way is to count the digits after the '.' sign, but that looks quite clumsy to me. Is there a possibility to get the information needed out of a float or Decimal object?

SOLUTION (one of them, of course :) )

I chose to use the python decimal.Decimal class to help me with my problem:

e = abs(Decimal(string_value).as_tuple().exponent)

NOTE: this only works when the parameter from which the Decimal is constructed is a string and not a float (which would lead to floating point inaccuracies).

Thanks a lot for all other contributions.


Solution 1:

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.

Solution 2:

"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.

Solution 3:

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.